2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1982, 1986, 1989, 1991, 1993
25 * The Regents of the University of California. All rights reserved.
26 * (c) UNIX System Laboratories, Inc.
27 * All or some portions of this file are derived from material licensed
28 * to the University of California by American Telephone and Telegraph
29 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
30 * the permission of UNIX System Laboratories, Inc.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)kern_descrip.c 8.8 (Berkeley) 2/14/95
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/filedesc.h>
66 #include <sys/kernel.h>
67 #include <sys/vnode_internal.h>
68 #include <sys/proc_internal.h>
69 #include <sys/kauth.h>
70 #include <sys/file_internal.h>
71 #include <sys/socket.h>
72 #include <sys/socketvar.h>
74 #include <sys/ioctl.h>
75 #include <sys/fcntl.h>
76 #include <sys/malloc.h>
78 #include <sys/syslog.h>
79 #include <sys/unistd.h>
80 #include <sys/resourcevar.h>
81 #include <sys/aio_kern.h>
83 #include <kern/lock.h>
85 #include <bsm/audit_kernel.h>
87 #include <sys/mount_internal.h>
88 #include <sys/kdebug.h>
89 #include <sys/sysproto.h>
91 #include <kern/kern_types.h>
92 #include <kern/kalloc.h>
93 #include <libkern/OSAtomic.h>
98 int fdopen(dev_t dev
, int mode
, int type
, struct proc
*p
);
99 int ogetdtablesize(struct proc
*p
, void *uap
, register_t
*retval
);
100 int finishdup(struct proc
* p
, struct filedesc
*fdp
, int old
, int new, register_t
*retval
);
102 int closef(struct fileglob
*fg
, struct proc
*p
);
103 int falloc_locked(struct proc
*p
, struct fileproc
**resultfp
, int *resultfd
, int locked
);
104 void fddrop(struct proc
*p
, int fd
);
105 int fdgetf_noref(struct proc
*p
, int fd
, struct fileproc
**resultfp
);
106 void fg_drop(struct fileproc
* fp
);
107 void fg_free(struct fileglob
*fg
);
108 void fg_ref(struct fileproc
* fp
);
109 int fp_getfpshm(struct proc
*p
, int fd
, struct fileproc
**resultfp
, struct pshmnode
**resultpshm
);
111 static int closef_finish(struct fileproc
*fp
, struct fileglob
*fg
, struct proc
*p
);
113 extern void file_lock_init(void);
114 extern int is_suser(void);
115 extern int kqueue_stat(struct fileproc
*fp
, struct stat
*st
, struct proc
*p
);
116 extern int soo_stat(struct socket
*so
, struct stat
*ub
);
117 extern int vn_path_package_check(vnode_t vp
, char *path
, int pathlen
, int *component
);
119 extern kauth_scope_t kauth_scope_fileop
;
121 #define f_flag f_fglob->fg_flag
122 #define f_type f_fglob->fg_type
123 #define f_msgcount f_fglob->fg_msgcount
124 #define f_cred f_fglob->fg_cred
125 #define f_ops f_fglob->fg_ops
126 #define f_offset f_fglob->fg_offset
127 #define f_data f_fglob->fg_data
129 * Descriptor management.
131 struct filelist filehead
; /* head of list of open files */
132 struct fmsglist fmsghead
; /* head of list of open files */
133 struct fmsglist fmsg_ithead
; /* head of list of open files */
134 int nfiles
; /* actual number of open files */
137 lck_grp_attr_t
* file_lck_grp_attr
;
138 lck_grp_t
* file_lck_grp
;
139 lck_attr_t
* file_lck_attr
;
141 lck_mtx_t
* uipc_lock
;
142 lck_mtx_t
* file_iterate_lcok
;
143 lck_mtx_t
* file_flist_lock
;
150 /* allocate file lock group attribute and group */
151 file_lck_grp_attr
= lck_grp_attr_alloc_init();
152 lck_grp_attr_setstat(file_lck_grp_attr
);
154 file_lck_grp
= lck_grp_alloc_init("file", file_lck_grp_attr
);
156 /* Allocate file lock attribute */
157 file_lck_attr
= lck_attr_alloc_init();
158 //lck_attr_setdebug(file_lck_attr);
160 uipc_lock
= lck_mtx_alloc_init(file_lck_grp
, file_lck_attr
);
161 file_iterate_lcok
= lck_mtx_alloc_init(file_lck_grp
, file_lck_attr
);
162 file_flist_lock
= lck_mtx_alloc_init(file_lck_grp
, file_lck_attr
);
170 proc_fdlock(struct proc
*p
)
172 lck_mtx_lock(&p
->p_fdmlock
);
176 proc_fdunlock(struct proc
*p
)
178 lck_mtx_unlock(&p
->p_fdmlock
);
182 * System calls on descriptors.
186 getdtablesize(struct proc
*p
, __unused
struct getdtablesize_args
*uap
, register_t
*retval
)
189 *retval
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
196 ogetdtablesize(struct proc
*p
, __unused
void *uap
, register_t
*retval
)
199 *retval
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, NOFILE
);
205 static __inline__
void
206 _fdrelse(struct filedesc
*fdp
, int fd
)
208 if (fd
< fdp
->fd_freefile
)
209 fdp
->fd_freefile
= fd
;
211 if (fd
> fdp
->fd_lastfile
)
212 panic("fdrelse: fd_lastfile inconsistent");
214 fdp
->fd_ofiles
[fd
] = NULL
;
215 fdp
->fd_ofileflags
[fd
] = 0;
217 while ((fd
= fdp
->fd_lastfile
) > 0 &&
218 fdp
->fd_ofiles
[fd
] == NULL
&&
219 !(fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
224 * Duplicate a file descriptor.
230 struct dup_args
*uap
;
233 register struct filedesc
*fdp
= p
->p_fd
;
234 register int old
= uap
->fd
;
239 if ( (error
= fp_lookup(p
, old
, &fp
, 1)) ) {
243 if ( (error
= fdalloc(p
, 0, &new)) ) {
244 fp_drop(p
, old
, fp
, 1);
248 error
= finishdup(p
, fdp
, old
, new, retval
);
249 fp_drop(p
, old
, fp
, 1);
256 * Duplicate a file descriptor to a particular value.
262 struct dup2_args
*uap
;
265 register struct filedesc
*fdp
= p
->p_fd
;
266 register int old
= uap
->from
, new = uap
->to
;
272 if ( (error
= fp_lookup(p
, old
, &fp
, 1)) ) {
277 new >= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
279 fp_drop(p
, old
, fp
, 1);
284 fp_drop(p
, old
, fp
, 1);
289 if (new < 0 || new >= fdp
->fd_nfiles
) {
290 if ( (error
= fdalloc(p
, new, &i
)) ) {
291 fp_drop(p
, old
, fp
, 1);
300 struct fileproc
**fpp
;
303 flags
= fdp
->fd_ofileflags
[new];
304 if ((flags
& (UF_RESERVED
| UF_CLOSING
)) == UF_RESERVED
) {
305 fp_drop(p
, old
, fp
, 1);
311 * dup2() must succeed even if the close has an error.
313 if (*(fpp
= &fdp
->fd_ofiles
[new])) {
314 struct fileproc
*nfp
= *fpp
;
316 close_internal(p
, new, nfp
, (CLOSEINT_LOCKED
| CLOSEINT_WAITONCLOSE
| CLOSEINT_NOFDRELSE
| CLOSEINT_NOFDNOREF
));
320 error
= finishdup(p
, fdp
, old
, new, retval
);
321 fp_drop(p
, old
, fp
, 1);
328 * The file control system call.
331 fcntl(p
, uap
, retval
)
333 struct fcntl_args
*uap
;
337 struct filedesc
*fdp
= p
->p_fd
;
341 int i
, tmp
, error
, error2
, flg
= F_POSIX
;
343 struct vfs_context context
;
347 int devBlockSize
= 0;
351 AUDIT_ARG(fd
, uap
->fd
);
352 AUDIT_ARG(cmd
, uap
->cmd
);
355 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
360 context
.vc_ucred
= fp
->f_cred
;
361 if (proc_is64bit(p
)) {
365 /* since the arg parameter is defined as a long but may be either
366 * a long or a pointer we must take care to handle sign extension
367 * issues. Our sys call munger will sign extend a long when we are
368 * called from a 32-bit process. Since we can never have an address
369 * greater than 32-bits from a 32-bit process we lop off the top
370 * 32-bits to avoid getting the wrong address
372 argp
= CAST_USER_ADDR_T(uap
->arg
);
375 pop
= &fdp
->fd_ofileflags
[fd
];
380 newmin
= CAST_DOWN(int, uap
->arg
);
381 if ((u_int
)newmin
>= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
382 newmin
>= maxfiles
) {
386 if ( (error
= fdalloc(p
, newmin
, &i
)) )
388 error
= finishdup(p
, fdp
, fd
, i
, retval
);
392 *retval
= (*pop
& UF_EXCLOSE
)? 1 : 0;
397 *pop
= (*pop
&~ UF_EXCLOSE
) |
398 (uap
->arg
& 1)? UF_EXCLOSE
: 0;
403 *retval
= OFLAGS(fp
->f_flag
);
408 fp
->f_flag
&= ~FCNTLFLAGS
;
409 tmp
= CAST_DOWN(int, uap
->arg
);
410 fp
->f_flag
|= FFLAGS(tmp
) & FCNTLFLAGS
;
411 tmp
= fp
->f_flag
& FNONBLOCK
;
412 error
= fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, p
);
415 tmp
= fp
->f_flag
& FASYNC
;
416 error
= fo_ioctl(fp
, FIOASYNC
, (caddr_t
)&tmp
, p
);
419 fp
->f_flag
&= ~FNONBLOCK
;
421 (void)fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, p
);
425 if (fp
->f_type
== DTYPE_SOCKET
) {
426 *retval
= ((struct socket
*)fp
->f_data
)->so_pgid
;
430 error
= fo_ioctl(fp
, (int)TIOCGPGRP
, (caddr_t
)retval
, p
);
435 tmp
= CAST_DOWN(pid_t
, uap
->arg
);
436 if (fp
->f_type
== DTYPE_SOCKET
) {
437 ((struct socket
*)fp
->f_data
)->so_pgid
= tmp
;
441 if (fp
->f_type
== DTYPE_PIPE
) {
442 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, p
);
449 struct proc
*p1
= pfind(tmp
);
454 tmp
= (int)p1
->p_pgrp
->pg_id
;
456 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, p
);
461 /* Fall into F_SETLK */
464 if (fp
->f_type
!= DTYPE_VNODE
) {
468 vp
= (struct vnode
*)fp
->f_data
;
471 offset
= fp
->f_offset
;
474 /* Copy in the lock structure */
475 error
= copyin(argp
, (caddr_t
)&fl
, sizeof (fl
));
479 if ( (error
= vnode_getwithref(vp
)) ) {
482 if (fl
.l_whence
== SEEK_CUR
)
483 fl
.l_start
+= offset
;
488 if ((fflag
& FREAD
) == 0) {
493 OSBitOrAtomic(P_LADVLOCK
, &p
->p_ladvflag
);
494 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_SETLK
, &fl
, flg
, &context
);
499 if ((fflag
& FWRITE
) == 0) {
504 OSBitOrAtomic(P_LADVLOCK
, &p
->p_ladvflag
);
505 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_SETLK
, &fl
, flg
, &context
);
510 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &fl
,
522 if (fp
->f_type
!= DTYPE_VNODE
) {
526 vp
= (struct vnode
*)fp
->f_data
;
528 offset
= fp
->f_offset
;
531 /* Copy in the lock structure */
532 error
= copyin(argp
, (caddr_t
)&fl
, sizeof (fl
));
536 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
537 if (fl
.l_whence
== SEEK_CUR
)
538 fl
.l_start
+= offset
;
540 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_GETLK
, &fl
, F_POSIX
, &context
);
545 error
= copyout((caddr_t
)&fl
, argp
, sizeof (fl
));
549 case F_PREALLOCATE
: {
550 fstore_t alloc_struct
; /* structure for allocate command */
551 u_int32_t alloc_flags
= 0;
553 if (fp
->f_type
!= DTYPE_VNODE
) {
558 vp
= (struct vnode
*)fp
->f_data
;
561 /* make sure that we have write permission */
562 if ((fp
->f_flag
& FWRITE
) == 0) {
567 error
= copyin(argp
, (caddr_t
)&alloc_struct
, sizeof (alloc_struct
));
571 /* now set the space allocated to 0 */
572 alloc_struct
.fst_bytesalloc
= 0;
575 * Do some simple parameter checking
578 /* set up the flags */
580 alloc_flags
|= PREALLOCATE
;
582 if (alloc_struct
.fst_flags
& F_ALLOCATECONTIG
)
583 alloc_flags
|= ALLOCATECONTIG
;
585 if (alloc_struct
.fst_flags
& F_ALLOCATEALL
)
586 alloc_flags
|= ALLOCATEALL
;
589 * Do any position mode specific stuff. The only
590 * position mode supported now is PEOFPOSMODE
593 switch (alloc_struct
.fst_posmode
) {
596 if (alloc_struct
.fst_offset
!= 0) {
601 alloc_flags
|= ALLOCATEFROMPEOF
;
605 if (alloc_struct
.fst_offset
<= 0) {
610 alloc_flags
|= ALLOCATEFROMVOL
;
618 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
620 * call allocate to get the space
622 error
= VNOP_ALLOCATE(vp
,alloc_struct
.fst_length
,alloc_flags
,
623 &alloc_struct
.fst_bytesalloc
, alloc_struct
.fst_offset
,
627 error2
= copyout((caddr_t
)&alloc_struct
, argp
, sizeof (alloc_struct
));
636 if (fp
->f_type
!= DTYPE_VNODE
) {
642 error
= copyin(argp
, (caddr_t
)&offset
, sizeof (off_t
));
647 * Make sure that we are root. Growing a file
648 * without zero filling the data is a security hole
649 * root would have access anyway so we'll allow it
656 vp
= (struct vnode
*)fp
->f_data
;
658 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
662 error
= vnode_setsize(vp
, offset
, IO_NOZEROFILL
, &context
);
669 if (fp
->f_type
!= DTYPE_VNODE
) {
673 vp
= (struct vnode
*)fp
->f_data
;
676 if ( (error
= vnode_getwithref(vp
)) == 0) {
678 vnode_clearnoreadahead(vp
);
680 vnode_setnoreadahead(vp
);
687 if (fp
->f_type
!= DTYPE_VNODE
) {
691 vp
= (struct vnode
*)fp
->f_data
;
694 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
696 vnode_setnocache(vp
);
698 vnode_clearnocache(vp
);
704 case F_GLOBAL_NOCACHE
:
705 if (fp
->f_type
!= DTYPE_VNODE
) {
709 vp
= (struct vnode
*)fp
->f_data
;
712 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
714 *retval
= vnode_isnocache(vp
);
717 vnode_setnocache(vp
);
719 vnode_clearnocache(vp
);
726 struct radvisory ra_struct
;
728 if (fp
->f_type
!= DTYPE_VNODE
) {
732 vp
= (struct vnode
*)fp
->f_data
;
735 if ( (error
= copyin(argp
, (caddr_t
)&ra_struct
, sizeof (ra_struct
))) )
737 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
738 error
= VNOP_IOCTL(vp
, F_RDADVISE
, (caddr_t
)&ra_struct
, 0, &context
);
745 case F_READBOOTSTRAP
:
746 case F_WRITEBOOTSTRAP
: {
747 fbootstraptransfer_t fbt_struct
;
748 user_fbootstraptransfer_t user_fbt_struct
;
750 caddr_t boot_structp
;
752 if (fp
->f_type
!= DTYPE_VNODE
) {
756 vp
= (struct vnode
*)fp
->f_data
;
759 if (IS_64BIT_PROCESS(p
)) {
760 sizeof_struct
= sizeof(user_fbt_struct
);
761 boot_structp
= (caddr_t
) &user_fbt_struct
;
764 sizeof_struct
= sizeof(fbt_struct
);
765 boot_structp
= (caddr_t
) &fbt_struct
;
767 error
= copyin(argp
, boot_structp
, sizeof_struct
);
770 if ( (error
= vnode_getwithref(vp
)) ) {
773 if (uap
->cmd
== F_WRITEBOOTSTRAP
) {
775 * Make sure that we are root. Updating the
776 * bootstrap on a disk could be a security hole
784 if (strcmp(vnode_mount(vp
)->mnt_vfsstat
.f_fstypename
, "hfs") != 0) {
788 * call vnop_ioctl to handle the I/O
790 error
= VNOP_IOCTL(vp
, uap
->cmd
, boot_structp
, 0, &context
);
796 struct log2phys l2p_struct
; /* structure for allocate command */
798 if (fp
->f_type
!= DTYPE_VNODE
) {
802 vp
= (struct vnode
*)fp
->f_data
;
804 if ( (error
= vnode_getwithref(vp
)) ) {
807 error
= VNOP_OFFTOBLK(vp
, fp
->f_offset
, &lbn
);
812 error
= VNOP_BLKTOOFF(vp
, lbn
, &offset
);
817 devBlockSize
= vfs_devblocksize(vnode_mount(vp
));
819 error
= VNOP_BLOCKMAP(vp
, offset
, devBlockSize
, &bn
, NULL
, NULL
, 0, &context
);
824 l2p_struct
.l2p_flags
= 0; /* for now */
825 l2p_struct
.l2p_contigbytes
= 0; /* for now */
826 l2p_struct
.l2p_devoffset
= bn
* devBlockSize
;
827 l2p_struct
.l2p_devoffset
+= fp
->f_offset
- offset
;
828 error
= copyout((caddr_t
)&l2p_struct
, argp
, sizeof (l2p_struct
));
836 if (fp
->f_type
!= DTYPE_VNODE
) {
840 vp
= (struct vnode
*)fp
->f_data
;
843 pathlen
= MAXPATHLEN
;
844 MALLOC(pathbufp
, char *, pathlen
, M_TEMP
, M_WAITOK
);
845 if (pathbufp
== NULL
) {
849 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
850 error
= vn_getpath(vp
, pathbufp
, &pathlen
);
854 error
= copyout((caddr_t
)pathbufp
, argp
, pathlen
);
856 FREE(pathbufp
, M_TEMP
);
860 case F_PATHPKG_CHECK
: {
864 if (fp
->f_type
!= DTYPE_VNODE
) {
868 vp
= (struct vnode
*)fp
->f_data
;
871 pathlen
= MAXPATHLEN
;
872 pathbufp
= kalloc(MAXPATHLEN
);
874 if ( (error
= copyinstr(argp
, pathbufp
, MAXPATHLEN
, &pathlen
)) == 0 ) {
875 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
876 error
= vn_path_package_check(vp
, pathbufp
, pathlen
, retval
);
881 kfree(pathbufp
, MAXPATHLEN
);
885 case F_CHKCLEAN
: // used by regression tests to see if all dirty pages got cleaned by fsync()
886 case F_FULLFSYNC
: // fsync + flush the journal + DKIOCSYNCHRONIZECACHE
887 case F_FREEZE_FS
: // freeze all other fs operations for the fs of this fd
888 case F_THAW_FS
: { // thaw all frozen fs operations for the fs of this fd
889 if (fp
->f_type
!= DTYPE_VNODE
) {
893 vp
= (struct vnode
*)fp
->f_data
;
896 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
897 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
)NULL
, 0, &context
);
905 if (uap
->cmd
< FCNTL_FS_SPECIFIC_BASE
) {
910 // if it's a fs-specific fcntl() then just pass it through
912 if (fp
->f_type
!= DTYPE_VNODE
) {
916 vp
= (struct vnode
*)fp
->f_data
;
919 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
920 error
= VNOP_IOCTL(vp
, uap
->cmd
, CAST_DOWN(caddr_t
, argp
), 0, &context
);
929 AUDIT_ARG(vnpath_withref
, vp
, ARG_VNODE1
);
930 fp_drop(p
, fd
, fp
, 0);
933 fp_drop(p
, fd
, fp
, 1);
939 * Common code for dup, dup2, and fcntl(F_DUPFD).
942 finishdup(struct proc
* p
, struct filedesc
*fdp
, int old
, int new, register_t
*retval
)
944 struct fileproc
*nfp
;
945 struct fileproc
*ofp
;
947 if ((ofp
= fdp
->fd_ofiles
[old
]) == NULL
||
948 (fdp
->fd_ofileflags
[old
] & UF_RESERVED
)) {
955 MALLOC_ZONE(nfp
, struct fileproc
*, sizeof(struct fileproc
), M_FILEPROC
, M_WAITOK
);
956 bzero(nfp
, sizeof(struct fileproc
));
959 nfp
->f_flags
= ofp
->f_flags
;
960 nfp
->f_fglob
= ofp
->f_fglob
;
963 fdp
->fd_ofiles
[new] = nfp
;
964 fdp
->fd_ofileflags
[new] = fdp
->fd_ofileflags
[old
] &~ UF_EXCLOSE
;
965 if (new > fdp
->fd_lastfile
)
966 fdp
->fd_lastfile
= new;
973 close(struct proc
*p
, struct close_args
*uap
, __unused register_t
*retval
)
979 AUDIT_SYSCLOSE(p
, fd
);
983 if ( (error
= fp_lookup(p
,fd
,&fp
, 1)) ) {
988 error
= close_internal(p
, fd
, fp
, CLOSEINT_LOCKED
| CLOSEINT_WAITONCLOSE
);
997 * Close a file descriptor.
1000 close_internal(struct proc
*p
, int fd
, struct fileproc
*fp
, int flags
)
1002 struct filedesc
*fdp
= p
->p_fd
;
1004 int locked
= flags
& CLOSEINT_LOCKED
;
1005 int waitonclose
= flags
& CLOSEINT_WAITONCLOSE
;
1006 int norelse
= flags
& CLOSEINT_NOFDRELSE
;
1007 int nofdref
= flags
& CLOSEINT_NOFDNOREF
;
1008 int slpstate
= PRIBIO
;
1013 /* Keep people from using the filedesc while we are closing it */
1014 fdp
->fd_ofileflags
[fd
] |= UF_RESERVED
;
1016 fdp
->fd_ofileflags
[fd
] |= UF_CLOSING
;
1019 if ((waitonclose
&& ((fp
->f_flags
& FP_CLOSING
) == FP_CLOSING
))) {
1021 fp_drop(p
, fd
, fp
, 1);
1022 fp
->f_flags
|= FP_WAITCLOSE
;
1025 msleep(&fp
->f_flags
, &p
->p_fdmlock
, slpstate
, "close wait",0) ;
1029 fp
->f_flags
|= FP_CLOSING
;
1033 if ( (fp
->f_flags
& FP_AIOISSUED
) || kauth_authorize_fileop_has_listeners() ) {
1037 if ( (fp
->f_type
== DTYPE_VNODE
) && kauth_authorize_fileop_has_listeners() ) {
1039 * call out to allow 3rd party notification of close.
1040 * Ignore result of kauth_authorize_fileop call.
1042 if (vnode_getwithref((vnode_t
)fp
->f_data
) == 0) {
1043 u_int fileop_flags
= 0;
1044 if ((fp
->f_flags
& FP_WRITTEN
) != 0)
1045 fileop_flags
|= KAUTH_FILEOP_CLOSE_MODIFIED
;
1046 kauth_authorize_fileop(fp
->f_fglob
->fg_cred
, KAUTH_FILEOP_CLOSE
,
1047 (uintptr_t)fp
->f_data
, (uintptr_t)fileop_flags
);
1048 vnode_put((vnode_t
)fp
->f_data
);
1051 if (fp
->f_flags
& FP_AIOISSUED
)
1053 * cancel all async IO requests that can be cancelled.
1055 _aio_close( p
, fd
);
1060 if (fd
< fdp
->fd_knlistsize
)
1061 knote_fdclose(p
, fd
);
1063 if (fp
->f_flags
& FP_WAITEVENT
)
1064 (void)waitevent_close(p
, fp
);
1066 if ((fp
->f_flags
& FP_INCHRREAD
) == 0)
1067 fileproc_drain(p
, fp
);
1070 error
= closef_locked(fp
, fp
->f_fglob
, p
);
1071 if ((fp
->f_flags
& FP_WAITCLOSE
) == FP_WAITCLOSE
)
1072 wakeup(&fp
->f_flags
);
1073 fp
->f_flags
&= ~(FP_WAITCLOSE
| FP_CLOSING
);
1078 FREE_ZONE(fp
, sizeof *fp
, M_FILEPROC
);
1083 * Return status information about a file descriptor.
1085 * XXX switch on node type is bogus; need a stat in struct fileops instead.
1088 fstat1(struct proc
*p
, int fd
, user_addr_t ub
, user_addr_t xsecurity
, user_addr_t xsecurity_size
)
1090 struct fileproc
*fp
;
1092 struct user_stat user_sb
;
1097 kauth_filesec_t fsec
;
1098 ssize_t xsecurity_bufsize
;
1100 struct vfs_context context
;
1105 if ((error
= fp_lookup(p
, fd
, &fp
, 0)) != 0)
1109 fsec
= KAUTH_FILESEC_NONE
;
1114 context
.vc_proc
= current_proc();
1115 context
.vc_ucred
= kauth_cred_get();
1116 if ((error
= vnode_getwithref((vnode_t
)data
)) == 0) {
1118 * If the caller has the file open, and is not requesting extended security,
1119 * we are going to let them get the basic stat information.
1121 if (xsecurity
== USER_ADDR_NULL
) {
1122 error
= vn_stat_noauth((vnode_t
)data
, &sb
, NULL
, &context
);
1124 error
= vn_stat((vnode_t
)data
, &sb
, &fsec
, &context
);
1127 AUDIT_ARG(vnpath
, (struct vnode
*)data
, ARG_VNODE1
);
1128 (void)vnode_put((vnode_t
)data
);
1133 error
= soo_stat((struct socket
*)data
, &sb
);
1137 error
= pipe_stat((void *)data
, &sb
);
1141 error
= pshm_stat((void *)data
, &sb
);
1145 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1146 error
= kqueue_stat(fp
, &sb
, p
);
1147 thread_funnel_set(kernel_flock
, funnel_state
);
1154 /* Zap spare fields */
1156 sb
.st_qspare
[0] = 0LL;
1157 sb
.st_qspare
[1] = 0LL;
1160 if (IS_64BIT_PROCESS(current_proc())) {
1161 munge_stat(&sb
, &user_sb
);
1162 my_size
= sizeof(user_sb
);
1163 sbp
= (caddr_t
)&user_sb
;
1166 my_size
= sizeof(sb
);
1169 error
= copyout(sbp
, ub
, my_size
);
1172 /* caller wants extended security information? */
1173 if (xsecurity
!= USER_ADDR_NULL
) {
1175 /* did we get any? */
1176 if (fsec
== KAUTH_FILESEC_NONE
) {
1177 if (susize(xsecurity_size
, 0) != 0) {
1182 /* find the user buffer size */
1183 xsecurity_bufsize
= fusize(xsecurity_size
);
1185 /* copy out the actual data size */
1186 if (susize(xsecurity_size
, KAUTH_FILESEC_COPYSIZE(fsec
)) != 0) {
1191 /* if the caller supplied enough room, copy out to it */
1192 if (xsecurity_bufsize
>= KAUTH_FILESEC_COPYSIZE(fsec
))
1193 error
= copyout(fsec
, xsecurity
, KAUTH_FILESEC_COPYSIZE(fsec
));
1197 fp_drop(p
, fd
, fp
, 0);
1199 kauth_filesec_free(fsec
);
1204 fstat_extended(struct proc
*p
, struct fstat_extended_args
*uap
, __unused register_t
*retval
)
1206 return(fstat1(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
));
1210 fstat(struct proc
*p
, register struct fstat_args
*uap
, __unused register_t
*retval
)
1212 return(fstat1(p
, uap
->fd
, uap
->ub
, 0, 0));
1216 * Return pathconf information about a file descriptor.
1219 fpathconf(p
, uap
, retval
)
1221 register struct fpathconf_args
*uap
;
1225 struct fileproc
*fp
;
1227 struct vfs_context context
;
1233 AUDIT_ARG(fd
, uap
->fd
);
1234 if ( (error
= fp_lookup(p
, fd
, &fp
, 0)) )
1242 if (uap
->name
!= _PC_PIPE_BUF
) {
1256 vp
= (struct vnode
*)data
;
1258 if ( (error
= vnode_getwithref(vp
)) == 0) {
1259 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
1261 context
.vc_proc
= p
;
1262 context
.vc_ucred
= kauth_cred_get();
1264 error
= vn_pathconf(vp
, uap
->name
, retval
, &context
);
1266 (void)vnode_put(vp
);
1276 panic("fpathconf (unrecognized - %d)", type
);
1280 fp_drop(p
, fd
, fp
, 0);
1285 * Allocate a file descriptor for the process.
1290 fdalloc(p
, want
, result
)
1295 register struct filedesc
*fdp
= p
->p_fd
;
1297 int lim
, last
, numfiles
, oldnfiles
;
1298 struct fileproc
**newofiles
, **ofiles
;
1299 char *newofileflags
, *ofileflags
;
1302 * Search for a free descriptor starting at the higher
1303 * of want or fd_freefile. If that fails, consider
1304 * expanding the ofile array.
1306 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
1308 last
= min(fdp
->fd_nfiles
, lim
);
1309 if ((i
= want
) < fdp
->fd_freefile
)
1310 i
= fdp
->fd_freefile
;
1311 ofiles
= &fdp
->fd_ofiles
[i
];
1312 ofileflags
= &fdp
->fd_ofileflags
[i
];
1313 for (; i
< last
; i
++) {
1314 if (*ofiles
== NULL
&& !(*ofileflags
& UF_RESERVED
)) {
1315 *ofileflags
= UF_RESERVED
;
1316 if (i
> fdp
->fd_lastfile
)
1317 fdp
->fd_lastfile
= i
;
1318 if (want
<= fdp
->fd_freefile
)
1319 fdp
->fd_freefile
= i
;
1323 ofiles
++; ofileflags
++;
1327 * No space in current array. Expand?
1329 if (fdp
->fd_nfiles
>= lim
)
1331 if (fdp
->fd_nfiles
< NDEXTENT
)
1332 numfiles
= NDEXTENT
;
1334 numfiles
= 2 * fdp
->fd_nfiles
;
1339 MALLOC_ZONE(newofiles
, struct fileproc
**,
1340 numfiles
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
1342 if (newofiles
== NULL
) {
1345 if (fdp
->fd_nfiles
>= numfiles
) {
1346 FREE_ZONE(newofiles
, numfiles
* OFILESIZE
, M_OFILETABL
);
1349 newofileflags
= (char *) &newofiles
[numfiles
];
1351 * Copy the existing ofile and ofileflags arrays
1352 * and zero the new portion of each array.
1354 oldnfiles
= fdp
->fd_nfiles
;
1355 (void) memcpy(newofiles
, fdp
->fd_ofiles
,
1356 oldnfiles
* sizeof *fdp
->fd_ofiles
);
1357 (void) memset(&newofiles
[oldnfiles
], 0,
1358 (numfiles
- oldnfiles
) * sizeof *fdp
->fd_ofiles
);
1360 (void) memcpy(newofileflags
, fdp
->fd_ofileflags
,
1361 oldnfiles
* sizeof *fdp
->fd_ofileflags
);
1362 (void) memset(&newofileflags
[oldnfiles
], 0,
1363 (numfiles
- oldnfiles
) *
1364 sizeof *fdp
->fd_ofileflags
);
1365 ofiles
= fdp
->fd_ofiles
;
1366 fdp
->fd_ofiles
= newofiles
;
1367 fdp
->fd_ofileflags
= newofileflags
;
1368 fdp
->fd_nfiles
= numfiles
;
1369 FREE_ZONE(ofiles
, oldnfiles
* OFILESIZE
, M_OFILETABL
);
1375 * Check to see whether n user file descriptors
1376 * are available to the process p.
1383 struct filedesc
*fdp
= p
->p_fd
;
1384 struct fileproc
**fpp
;
1388 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
1389 if ((i
= lim
- fdp
->fd_nfiles
) > 0 && (n
-= i
) <= 0)
1391 fpp
= &fdp
->fd_ofiles
[fdp
->fd_freefile
];
1392 flags
= &fdp
->fd_ofileflags
[fdp
->fd_freefile
];
1393 for (i
= fdp
->fd_nfiles
- fdp
->fd_freefile
; --i
>= 0; fpp
++, flags
++)
1394 if (*fpp
== NULL
&& !(*flags
& UF_RESERVED
) && --n
<= 0)
1404 _fdrelse(p
->p_fd
, fd
);
1412 struct filedesc
*fdp
= p
->p_fd
;
1413 struct fileproc
*fp
;
1415 if (fd
< fdp
->fd_freefile
)
1416 fdp
->fd_freefile
= fd
;
1418 if (fd
> fdp
->fd_lastfile
)
1419 panic("fdrelse: fd_lastfile inconsistent");
1421 fp
= fdp
->fd_ofiles
[fd
];
1422 fdp
->fd_ofiles
[fd
] = NULL
;
1423 fdp
->fd_ofileflags
[fd
] = 0;
1425 while ((fd
= fdp
->fd_lastfile
) > 0 &&
1426 fdp
->fd_ofiles
[fd
] == NULL
&&
1427 !(fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
1429 FREE_ZONE(fp
, sizeof *fp
, M_FILEPROC
);
1434 fdgetf_noref(p
, fd
, resultfp
)
1437 struct fileproc
**resultfp
;
1439 struct filedesc
*fdp
= p
->p_fd
;
1440 struct fileproc
*fp
;
1442 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
1443 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
1444 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1453 /* should be called only when proc_fdlock is held */
1455 fp_setflags(proc_t p
, struct fileproc
* fp
, int flags
)
1458 fp
->f_flags
|= flags
;
1463 fp_clearflags(proc_t p
, struct fileproc
* fp
, int flags
)
1468 fp
->f_flags
&= ~flags
;
1473 fp_getfvp(p
, fd
, resultfp
, resultvp
)
1476 struct fileproc
**resultfp
;
1477 struct vnode
**resultvp
;
1479 struct filedesc
*fdp
= p
->p_fd
;
1480 struct fileproc
*fp
;
1483 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
1484 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
1485 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1489 if (fp
->f_type
!= DTYPE_VNODE
) {
1498 *resultvp
= (struct vnode
*)fp
->f_data
;
1506 * Returns: EBADF The file descriptor is invalid
1507 * EOPNOTSUPP The file descriptor is not a socket
1510 * Notes: EOPNOTSUPP should probably be ENOTSOCK; this function is only
1511 * ever called from accept1().
1514 fp_getfsock(p
, fd
, resultfp
, results
)
1517 struct fileproc
**resultfp
;
1518 struct socket
**results
;
1520 struct filedesc
*fdp
= p
->p_fd
;
1521 struct fileproc
*fp
;
1524 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
1525 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
1526 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1530 if (fp
->f_type
!= DTYPE_SOCKET
) {
1539 *results
= (struct socket
*)fp
->f_data
;
1547 fp_getfkq(p
, fd
, resultfp
, resultkq
)
1550 struct fileproc
**resultfp
;
1551 struct kqueue
**resultkq
;
1553 struct filedesc
*fdp
= p
->p_fd
;
1554 struct fileproc
*fp
;
1557 if ( fd
< 0 || fd
>= fdp
->fd_nfiles
||
1558 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
1559 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1563 if (fp
->f_type
!= DTYPE_KQUEUE
) {
1572 *resultkq
= (struct kqueue
*)fp
->f_data
;
1579 fp_getfpshm(p
, fd
, resultfp
, resultpshm
)
1582 struct fileproc
**resultfp
;
1583 struct pshmnode
**resultpshm
;
1585 struct filedesc
*fdp
= p
->p_fd
;
1586 struct fileproc
*fp
;
1589 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
1590 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
1591 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1595 if (fp
->f_type
!= DTYPE_PSXSHM
) {
1605 *resultpshm
= (struct pshmnode
*)fp
->f_data
;
1613 fp_getfpsem(p
, fd
, resultfp
, resultpsem
)
1616 struct fileproc
**resultfp
;
1617 struct psemnode
**resultpsem
;
1619 struct filedesc
*fdp
= p
->p_fd
;
1620 struct fileproc
*fp
;
1623 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
1624 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
1625 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1629 if (fp
->f_type
!= DTYPE_PSXSEM
) {
1638 *resultpsem
= (struct psemnode
*)fp
->f_data
;
1644 fp_lookup(p
, fd
, resultfp
, locked
)
1647 struct fileproc
**resultfp
;
1650 struct filedesc
*fdp
= p
->p_fd
;
1651 struct fileproc
*fp
;
1655 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
1656 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
1657 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1673 fp_drop_written(proc_t p
, int fd
, struct fileproc
*fp
)
1679 fp
->f_flags
|= FP_WRITTEN
;
1681 error
= fp_drop(p
, fd
, fp
, 1);
1690 fp_drop_event(proc_t p
, int fd
, struct fileproc
*fp
)
1696 fp
->f_flags
|= FP_WAITEVENT
;
1698 error
= fp_drop(p
, fd
, fp
, 1);
1706 fp_drop(p
, fd
, fp
, locked
)
1709 struct fileproc
*fp
;
1712 struct filedesc
*fdp
= p
->p_fd
;
1716 if ((fp
== FILEPROC_NULL
) && (fd
< 0 || fd
>= fdp
->fd_nfiles
||
1717 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
1718 ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
1719 !(fdp
->fd_ofileflags
[fd
] & UF_CLOSING
)))) {
1726 if (p
->p_fpdrainwait
&& fp
->f_iocount
== 0) {
1727 p
->p_fpdrainwait
= 0;
1728 wakeup(&p
->p_fpdrainwait
);
1737 file_vnode(int fd
, struct vnode
**vpp
)
1739 struct proc
* p
= current_proc();
1740 struct fileproc
*fp
;
1744 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
1748 if (fp
->f_type
!= DTYPE_VNODE
) {
1749 fp_drop(p
, fd
, fp
,1);
1753 *vpp
= (struct vnode
*)fp
->f_data
;
1761 file_socket(int fd
, struct socket
**sp
)
1763 struct proc
* p
= current_proc();
1764 struct fileproc
*fp
;
1768 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
1772 if (fp
->f_type
!= DTYPE_SOCKET
) {
1773 fp_drop(p
, fd
, fp
,1);
1777 *sp
= (struct socket
*)fp
->f_data
;
1784 file_flags(int fd
, int * flags
)
1787 struct proc
* p
= current_proc();
1788 struct fileproc
*fp
;
1792 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
1796 *flags
= (int)fp
->f_flag
;
1797 fp_drop(p
, fd
, fp
,1);
1807 struct fileproc
*fp
;
1808 struct proc
*p
= current_proc();
1811 if (fd
< 0 || fd
>= p
->p_fd
->fd_nfiles
||
1812 (fp
= p
->p_fd
->fd_ofiles
[fd
]) == NULL
||
1813 ((p
->p_fd
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
1814 !(p
->p_fd
->fd_ofileflags
[fd
] & UF_CLOSING
))) {
1820 if (p
->p_fpdrainwait
&& fp
->f_iocount
== 0) {
1821 p
->p_fpdrainwait
= 0;
1822 wakeup(&p
->p_fpdrainwait
);
1831 falloc(p
, resultfp
, resultfd
)
1833 struct fileproc
**resultfp
;
1839 error
= falloc_locked(p
, resultfp
, resultfd
, 1);
1845 * Create a new open file structure and allocate
1846 * a file decriptor for the process that refers to it.
1849 falloc_locked(p
, resultfp
, resultfd
, locked
)
1851 struct fileproc
**resultfp
;
1855 struct fileproc
*fp
, *fq
;
1856 struct fileglob
*fg
;
1861 if ( (error
= fdalloc(p
, 0, &nfd
)) ) {
1866 if (nfiles
>= maxfiles
) {
1873 * Allocate a new file descriptor.
1874 * If the process has file descriptor zero open, add to the list
1875 * of open files at that point, otherwise put it at the front of
1876 * the list of open files.
1880 MALLOC_ZONE(fp
, struct fileproc
*, sizeof(struct fileproc
), M_FILEPROC
, M_WAITOK
);
1881 MALLOC_ZONE(fg
, struct fileglob
*, sizeof(struct fileglob
), M_FILEGLOB
, M_WAITOK
);
1882 bzero(fp
, sizeof(struct fileproc
));
1883 bzero(fg
, sizeof(struct fileglob
));
1884 lck_mtx_init(&fg
->fg_lock
, file_lck_grp
, file_lck_attr
);
1892 fp
->f_cred
= kauth_cred_proc_ref(p
);
1894 lck_mtx_lock(file_flist_lock
);
1898 if ( (fq
= p
->p_fd
->fd_ofiles
[0]) ) {
1899 LIST_INSERT_AFTER(fq
->f_fglob
, fg
, f_list
);
1901 LIST_INSERT_HEAD(&filehead
, fg
, f_list
);
1903 lck_mtx_unlock(file_flist_lock
);
1905 p
->p_fd
->fd_ofiles
[nfd
] = fp
;
1919 * Free a file structure.
1923 struct fileglob
*fg
;
1927 lck_mtx_lock(file_flist_lock
);
1928 LIST_REMOVE(fg
, f_list
);
1930 lck_mtx_unlock(file_flist_lock
);
1933 if (cred
!= NOCRED
) {
1934 fg
->fg_cred
= NOCRED
;
1935 kauth_cred_rele(cred
);
1937 lck_mtx_destroy(&fg
->fg_lock
, file_lck_grp
);
1939 FREE_ZONE(fg
, sizeof *fg
, M_FILEGLOB
);
1946 struct filedesc
*fdp
= p
->p_fd
;
1947 int i
= fdp
->fd_lastfile
;
1948 struct fileproc
**fpp
= &fdp
->fd_ofiles
[i
];
1949 char *flags
= &fdp
->fd_ofileflags
[i
];
1952 funnel_state
= thread_funnel_set(kernel_flock
, FALSE
);
1956 if ((*flags
& (UF_RESERVED
|UF_EXCLOSE
)) == UF_EXCLOSE
) {
1957 struct fileproc
*fp
= *fpp
;
1959 if (i
< fdp
->fd_knlistsize
)
1960 knote_fdclose(p
, i
);
1962 *fpp
= NULL
; *flags
= 0;
1963 if (i
== fdp
->fd_lastfile
&& i
> 0)
1965 closef_locked(fp
, fp
->f_fglob
, p
);
1966 FREE_ZONE(fp
, sizeof *fp
, M_FILEPROC
);
1969 i
--; fpp
--; flags
--;
1972 thread_funnel_set(kernel_flock
, funnel_state
);
1976 * Copy a filedesc structure.
1982 struct filedesc
*newfdp
, *fdp
= p
->p_fd
;
1984 struct fileproc
*ofp
, *fp
;
1987 MALLOC_ZONE(newfdp
, struct filedesc
*,
1988 sizeof *newfdp
, M_FILEDESC
, M_WAITOK
);
1995 * the FD_CHROOT flag will be inherited via this copy
1997 (void) memcpy(newfdp
, fdp
, sizeof *newfdp
);
2000 * for both fd_cdir and fd_rdir make sure we get
2001 * a valid reference... if we can't, than set
2002 * set the pointer(s) to NULL in the child... this
2003 * will keep us from using a non-referenced vp
2004 * and allows us to do the vnode_rele only on
2005 * a properly referenced vp
2007 if ( (v_dir
= newfdp
->fd_cdir
) ) {
2008 if (vnode_getwithref(v_dir
) == 0) {
2009 if ( (vnode_ref(v_dir
)) )
2010 newfdp
->fd_cdir
= NULL
;
2013 newfdp
->fd_cdir
= NULL
;
2015 if (newfdp
->fd_cdir
== NULL
&& fdp
->fd_cdir
) {
2017 * we couldn't get a new reference on
2018 * the current working directory being
2019 * inherited... we might as well drop
2020 * our reference from the parent also
2021 * since the vnode has gone DEAD making
2022 * it useless... by dropping it we'll
2023 * be that much closer to recyling it
2025 vnode_rele(fdp
->fd_cdir
);
2026 fdp
->fd_cdir
= NULL
;
2029 if ( (v_dir
= newfdp
->fd_rdir
) ) {
2030 if (vnode_getwithref(v_dir
) == 0) {
2031 if ( (vnode_ref(v_dir
)) )
2032 newfdp
->fd_rdir
= NULL
;
2035 newfdp
->fd_rdir
= NULL
;
2037 if (newfdp
->fd_rdir
== NULL
&& fdp
->fd_rdir
) {
2039 * we couldn't get a new reference on
2040 * the root directory being
2041 * inherited... we might as well drop
2042 * our reference from the parent also
2043 * since the vnode has gone DEAD making
2044 * it useless... by dropping it we'll
2045 * be that much closer to recyling it
2047 vnode_rele(fdp
->fd_rdir
);
2048 fdp
->fd_rdir
= NULL
;
2050 newfdp
->fd_refcnt
= 1;
2053 * If the number of open files fits in the internal arrays
2054 * of the open file structure, use them, otherwise allocate
2055 * additional memory for the number of descriptors currently
2058 if (newfdp
->fd_lastfile
< NDFILE
)
2062 * Compute the smallest multiple of NDEXTENT needed
2063 * for the file descriptors currently in use,
2064 * allowing the table to shrink.
2066 i
= newfdp
->fd_nfiles
;
2067 while (i
> 2 * NDEXTENT
&& i
> newfdp
->fd_lastfile
* 2)
2072 MALLOC_ZONE(newfdp
->fd_ofiles
, struct fileproc
**,
2073 i
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
2074 if (newfdp
->fd_ofiles
== NULL
) {
2075 if (newfdp
->fd_cdir
)
2076 vnode_rele(newfdp
->fd_cdir
);
2077 if (newfdp
->fd_rdir
)
2078 vnode_rele(newfdp
->fd_rdir
);
2080 FREE_ZONE(newfdp
, sizeof *newfdp
, M_FILEDESC
);
2085 newfdp
->fd_ofileflags
= (char *) &newfdp
->fd_ofiles
[i
];
2086 newfdp
->fd_nfiles
= i
;
2088 if (fdp
->fd_nfiles
> 0) {
2089 struct fileproc
**fpp
;
2092 (void) memcpy(newfdp
->fd_ofiles
, fdp
->fd_ofiles
,
2093 i
* sizeof *fdp
->fd_ofiles
);
2094 (void) memcpy(newfdp
->fd_ofileflags
, fdp
->fd_ofileflags
,
2095 i
* sizeof *fdp
->fd_ofileflags
);
2098 * kq descriptors cannot be copied.
2100 if (newfdp
->fd_knlistsize
!= -1) {
2101 fpp
= &newfdp
->fd_ofiles
[newfdp
->fd_lastfile
];
2102 for (i
= newfdp
->fd_lastfile
; i
>= 0; i
--, fpp
--) {
2103 if (*fpp
!= NULL
&& (*fpp
)->f_type
== DTYPE_KQUEUE
) {
2105 if (i
< newfdp
->fd_freefile
)
2106 newfdp
->fd_freefile
= i
;
2108 if (*fpp
== NULL
&& i
== newfdp
->fd_lastfile
&& i
> 0)
2109 newfdp
->fd_lastfile
--;
2111 newfdp
->fd_knlist
= NULL
;
2112 newfdp
->fd_knlistsize
= -1;
2113 newfdp
->fd_knhash
= NULL
;
2114 newfdp
->fd_knhashmask
= 0;
2116 fpp
= newfdp
->fd_ofiles
;
2117 flags
= newfdp
->fd_ofileflags
;
2119 for (i
= newfdp
->fd_lastfile
; i
-- >= 0; fpp
++, flags
++)
2120 if ((ofp
= *fpp
) != NULL
&& !(*flags
& UF_RESERVED
)) {
2121 MALLOC_ZONE(fp
, struct fileproc
*, sizeof(struct fileproc
), M_FILEPROC
, M_WAITOK
);
2122 bzero(fp
, sizeof(struct fileproc
));
2123 fp
->f_flags
= ofp
->f_flags
;
2124 //fp->f_iocount = ofp->f_iocount;
2126 fp
->f_fglob
= ofp
->f_fglob
;
2134 (void) memset(newfdp
->fd_ofiles
, 0, i
* OFILESIZE
);
2141 * Release a filedesc structure.
2147 struct filedesc
*fdp
;
2148 struct fileproc
*fp
;
2153 /* Certain daemons might not have file descriptors */
2156 if ((fdp
== NULL
) || (--fdp
->fd_refcnt
> 0)) {
2160 if (fdp
->fd_refcnt
== 0xffff)
2161 panic("fdfree: bad fd_refcnt");
2163 /* Last reference: the structure can't change out from under us */
2165 if (fdp
->fd_nfiles
> 0 && fdp
->fd_ofiles
) {
2166 for (i
= fdp
->fd_lastfile
; i
>= 0; i
--) {
2167 if ((fp
= fdp
->fd_ofiles
[i
]) != NULL
) {
2169 if (fdp
->fd_ofileflags
[i
] & UF_RESERVED
)
2170 panic("fdfree: found fp with UF_RESERVED\n");
2172 /* closef drops the iocount ... */
2173 if ((fp
->f_flags
& FP_INCHRREAD
) != 0)
2175 fdp
->fd_ofiles
[i
] = NULL
;
2176 fdp
->fd_ofileflags
[i
] |= UF_RESERVED
;
2178 if (i
< fdp
->fd_knlistsize
)
2179 knote_fdclose(p
, i
);
2180 if (fp
->f_flags
& FP_WAITEVENT
)
2181 (void)waitevent_close(p
, fp
);
2182 (void) closef_locked(fp
, fp
->f_fglob
, p
);
2183 FREE_ZONE(fp
, sizeof *fp
, M_FILEPROC
);
2186 FREE_ZONE(fdp
->fd_ofiles
, fdp
->fd_nfiles
* OFILESIZE
, M_OFILETABL
);
2187 fdp
->fd_ofiles
= NULL
;
2194 vnode_rele(fdp
->fd_cdir
);
2196 vnode_rele(fdp
->fd_rdir
);
2203 FREE(fdp
->fd_knlist
, M_KQUEUE
);
2205 FREE(fdp
->fd_knhash
, M_KQUEUE
);
2207 FREE_ZONE(fdp
, sizeof *fdp
, M_FILEDESC
);
2211 closef_finish(fp
, fg
, p
)
2212 struct fileproc
*fp
;
2213 struct fileglob
*fg
;
2219 struct vfs_context context
;
2221 if ((fg
->fg_flag
& FHASLOCK
) && fg
->fg_type
== DTYPE_VNODE
) {
2222 lf
.l_whence
= SEEK_SET
;
2225 lf
.l_type
= F_UNLCK
;
2226 vp
= (struct vnode
*)fg
->fg_data
;
2227 context
.vc_proc
= p
;
2228 context
.vc_ucred
= fg
->fg_cred
;
2230 (void) VNOP_ADVLOCK(vp
, (caddr_t
)fg
, F_UNLCK
, &lf
, F_FLOCK
, &context
);
2233 error
= fo_close(fg
, p
);
2237 if (((fp
!= (struct fileproc
*)0) && ((fp
->f_flags
& FP_INCHRREAD
) != 0))) {
2239 if ( ((fp
->f_flags
& FP_INCHRREAD
) != 0) ) {
2240 fileproc_drain(p
, fp
);
2251 struct fileglob
*fg
;
2257 error
= closef_locked((struct fileproc
*)0, fg
, p
);
2263 * Internal form of close.
2264 * Decrement reference count on file structure.
2265 * Note: p may be NULL when closing a file
2266 * that was being passed in a message.
2269 closef_locked(fp
, fg
, p
)
2270 struct fileproc
*fp
;
2271 struct fileglob
*fg
;
2276 struct vfs_context context
;
2283 * POSIX record locking dictates that any close releases ALL
2284 * locks owned by this process. This is handled by setting
2285 * a flag in the unlock to free ONLY locks obeying POSIX
2286 * semantics, and not to free BSD-style file locks.
2287 * If the descriptor was in a message, POSIX-style locks
2288 * aren't passed with the descriptor.
2290 if (p
&& (p
->p_ladvflag
& P_LADVLOCK
) && fg
->fg_type
== DTYPE_VNODE
) {
2293 lf
.l_whence
= SEEK_SET
;
2296 lf
.l_type
= F_UNLCK
;
2297 vp
= (struct vnode
*)fg
->fg_data
;
2299 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
2300 context
.vc_proc
= p
;
2301 context
.vc_ucred
= fg
->fg_cred
;
2302 (void) VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &lf
, F_POSIX
, &context
);
2304 (void)vnode_put(vp
);
2308 lck_mtx_lock(&fg
->fg_lock
);
2311 if (fg
->fg_count
> 0) {
2312 lck_mtx_unlock(&fg
->fg_lock
);
2315 if (fg
->fg_count
!= 0)
2316 panic("fg: being freed with bad fg_count (%d)", fg
, fg
->fg_count
);
2318 if (fp
&& (fp
->f_flags
& FP_WRITTEN
))
2319 fg
->fg_flag
|= FWASWRITTEN
;
2321 fg
->fg_lflags
|= FG_TERM
;
2322 lck_mtx_unlock(&fg
->fg_lock
);
2325 error
= closef_finish(fp
, fg
, p
);
2334 fileproc_drain(struct proc
*p
, struct fileproc
* fp
)
2336 fp
->f_iocount
-- ; /* (the one the close holds) */
2338 while (fp
->f_iocount
) {
2339 if (((fp
->f_flags
& FP_INSELECT
)== FP_INSELECT
)) {
2340 wait_queue_wakeup_all((wait_queue_t
)fp
->f_waddr
, &selwait
, THREAD_INTERRUPTED
);
2342 if (fp
->f_fglob
->fg_ops
->fo_drain
) {
2343 (*fp
->f_fglob
->fg_ops
->fo_drain
)(fp
, p
);
2346 p
->p_fpdrainwait
= 1;
2348 msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
, PRIBIO
, "fpdrain",0);
2350 //panic("successful wait after drain\n");
2355 fp_free(struct proc
* p
, int fd
, struct fileproc
* fp
)
2361 fg_free(fp
->f_fglob
);
2362 FREE_ZONE(fp
, sizeof *fp
, M_FILEPROC
);
2367 * Apply an advisory lock on a file descriptor.
2369 * Just attempt to get a record lock of the requested type on
2370 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
2373 flock(struct proc
*p
, register struct flock_args
*uap
, __unused register_t
*retval
)
2377 struct fileproc
*fp
;
2380 struct vfs_context context
;
2383 AUDIT_ARG(fd
, uap
->fd
);
2384 if ( (error
= fp_getfvp(p
, fd
, &fp
, &vp
)) ) {
2387 if ( (error
= vnode_getwithref(vp
)) ) {
2390 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
2392 context
.vc_proc
= p
;
2393 context
.vc_ucred
= fp
->f_cred
;
2395 lf
.l_whence
= SEEK_SET
;
2398 if (how
& LOCK_UN
) {
2399 lf
.l_type
= F_UNLCK
;
2400 fp
->f_flag
&= ~FHASLOCK
;
2401 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_UNLCK
, &lf
, F_FLOCK
, &context
);
2405 lf
.l_type
= F_WRLCK
;
2406 else if (how
& LOCK_SH
)
2407 lf
.l_type
= F_RDLCK
;
2412 fp
->f_flag
|= FHASLOCK
;
2413 if (how
& LOCK_NB
) {
2414 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_SETLK
, &lf
, F_FLOCK
, &context
);
2417 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_SETLK
, &lf
, F_FLOCK
|F_WAIT
, &context
);
2419 (void)vnode_put(vp
);
2421 fp_drop(p
, fd
, fp
, 0);
2427 * File Descriptor pseudo-device driver (/dev/fd/).
2429 * Opening minor device N dup()s the file (if any) connected to file
2430 * descriptor N belonging to the calling process. Note that this driver
2431 * consists of only the ``open()'' routine, because all subsequent
2432 * references to this file will be direct to the other driver.
2435 fdopen(dev_t dev
, __unused
int mode
, __unused
int type
, struct proc
*p
)
2439 * XXX Kludge: set curproc->p_dupfd to contain the value of the
2440 * the file descriptor being sought for duplication. The error
2441 * return ensures that the vnode for this device will be released
2442 * by vn_open. Open will detect this special error and take the
2443 * actions in dupfdopen below. Other callers of vn_open or vnop_open
2444 * will simply report the error.
2446 p
->p_dupfd
= minor(dev
);
2451 * Duplicate the specified descriptor to a free descriptor.
2454 dupfdopen(fdp
, indx
, dfd
, mode
, error
)
2455 register struct filedesc
*fdp
;
2456 register int indx
, dfd
;
2460 struct fileproc
*wfp
;
2461 struct fileproc
*fp
;
2462 struct proc
* p
= current_proc();
2465 * If the to-be-dup'd fd number is greater than the allowed number
2466 * of file descriptors, or the fd to be dup'd has already been
2467 * closed, reject. Note, check for new == old is necessary as
2468 * falloc could allocate an already closed to-be-dup'd descriptor
2469 * as the new descriptor.
2473 fp
= fdp
->fd_ofiles
[indx
];
2474 if (dfd
< 0 || dfd
>= fdp
->fd_nfiles
||
2475 (wfp
= fdp
->fd_ofiles
[dfd
]) == NULL
|| wfp
== fp
||
2476 (fdp
->fd_ofileflags
[dfd
] & UF_RESERVED
)) {
2482 * There are two cases of interest here.
2484 * For ENODEV simply dup (dfd) to file descriptor
2485 * (indx) and return.
2487 * For ENXIO steal away the file structure from (dfd) and
2488 * store it in (indx). (dfd) is effectively closed by
2491 * Any other error code is just returned.
2496 * Check that the mode the file is being opened for is a
2497 * subset of the mode of the existing descriptor.
2499 if (((mode
& (FREAD
|FWRITE
)) | wfp
->f_flag
) != wfp
->f_flag
) {
2503 if (indx
> fdp
->fd_lastfile
)
2504 fdp
->fd_lastfile
= indx
;
2508 fg_free(fp
->f_fglob
);
2509 fp
->f_fglob
= wfp
->f_fglob
;
2511 fdp
->fd_ofileflags
[indx
] = fdp
->fd_ofileflags
[dfd
];
2518 * Steal away the file pointer from dfd, and stuff it into indx.
2520 if (indx
> fdp
->fd_lastfile
)
2521 fdp
->fd_lastfile
= indx
;
2524 fg_free(fp
->f_fglob
);
2525 fp
->f_fglob
= wfp
->f_fglob
;
2527 fdp
->fd_ofileflags
[indx
] = fdp
->fd_ofileflags
[dfd
];
2532 FREE_ZONE(wfp
, sizeof *fp
, M_FILEPROC
);
2544 fg_ref(struct fileproc
* fp
)
2546 struct fileglob
*fg
;
2550 lck_mtx_lock(&fg
->fg_lock
);
2552 lck_mtx_unlock(&fg
->fg_lock
);
2556 fg_drop(struct fileproc
* fp
)
2558 struct fileglob
*fg
;
2561 lck_mtx_lock(&fg
->fg_lock
);
2563 lck_mtx_unlock(&fg
->fg_lock
);
2568 fg_insertuipc(struct fileglob
* fg
)
2572 lck_mtx_lock(&fg
->fg_lock
);
2574 while (fg
->fg_lflags
& FG_RMMSGQ
) {
2575 fg
->fg_lflags
|= FG_WRMMSGQ
;
2576 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_insertuipc", 0);
2581 if (fg
->fg_msgcount
== 1) {
2582 fg
->fg_lflags
|= FG_INSMSGQ
;
2585 lck_mtx_unlock(&fg
->fg_lock
);
2588 lck_mtx_lock(uipc_lock
);
2589 LIST_INSERT_HEAD(&fmsghead
, fg
, f_msglist
);
2590 lck_mtx_unlock(uipc_lock
);
2591 lck_mtx_lock(&fg
->fg_lock
);
2592 fg
->fg_lflags
&= ~FG_INSMSGQ
;
2593 if (fg
->fg_lflags
& FG_WINSMSGQ
) {
2594 fg
->fg_lflags
&= ~FG_WINSMSGQ
;
2595 wakeup(&fg
->fg_lflags
);
2597 lck_mtx_unlock(&fg
->fg_lock
);
2603 fg_removeuipc(struct fileglob
* fg
)
2607 lck_mtx_lock(&fg
->fg_lock
);
2608 while (fg
->fg_lflags
& FG_INSMSGQ
) {
2609 fg
->fg_lflags
|= FG_WINSMSGQ
;
2610 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_removeuipc", 0);
2613 if (fg
->fg_msgcount
== 0) {
2614 fg
->fg_lflags
|= FG_RMMSGQ
;
2617 lck_mtx_unlock(&fg
->fg_lock
);
2620 lck_mtx_lock(uipc_lock
);
2621 LIST_REMOVE(fg
, f_msglist
);
2622 lck_mtx_unlock(uipc_lock
);
2623 lck_mtx_lock(&fg
->fg_lock
);
2624 fg
->fg_lflags
&= ~FG_RMMSGQ
;
2625 if (fg
->fg_lflags
& FG_WRMMSGQ
) {
2626 fg
->fg_lflags
&= ~FG_WRMMSGQ
;
2627 wakeup(&fg
->fg_lflags
);
2629 lck_mtx_unlock(&fg
->fg_lock
);
2635 fo_read(struct fileproc
*fp
, struct uio
*uio
, kauth_cred_t cred
, int flags
, struct proc
*p
)
2637 return ((*fp
->f_ops
->fo_read
)(fp
, uio
, cred
, flags
, p
));
2641 fo_write(struct fileproc
*fp
, struct uio
*uio
, kauth_cred_t cred
, int flags
, struct proc
*p
)
2643 return((*fp
->f_ops
->fo_write
)(fp
, uio
, cred
, flags
, p
));
2647 fo_ioctl(struct fileproc
*fp
, u_long com
, caddr_t data
, struct proc
*p
)
2652 error
= (*fp
->f_ops
->fo_ioctl
)(fp
, com
, data
, p
);
2658 fo_select(struct fileproc
*fp
, int which
, void *wql
, struct proc
*p
)
2660 return((*fp
->f_ops
->fo_select
)(fp
, which
, wql
, p
));
2664 fo_close(struct fileglob
*fg
, struct proc
*p
)
2666 return((*fg
->fg_ops
->fo_close
)(fg
, p
));
2670 fo_kqfilter(struct fileproc
*fp
, struct knote
*kn
, struct proc
*p
)
2672 return ((*fp
->f_ops
->fo_kqfilter
)(fp
, kn
, p
));