2 * Copyright (c) 2000-2001 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.h>
70 #include <sys/socket.h>
71 #include <sys/socketvar.h>
73 #include <sys/ioctl.h>
74 #include <sys/fcntl.h>
75 #include <sys/malloc.h>
76 #include <sys/syslog.h>
77 #include <sys/unistd.h>
78 #include <sys/resourcevar.h>
80 #include <sys/mount.h>
83 * Descriptor management.
85 struct filelist filehead
; /* head of list of open files */
86 int nfiles
; /* actual number of open files */
89 * System calls on descriptors.
93 getdtablesize(p
, uap
, retval
)
98 *retval
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
104 ogetdtablesize(p
, uap
, retval
)
109 *retval
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, NOFILE
);
114 void _fdrelse(fdp
, fd
)
115 register struct filedesc
*fdp
;
118 if (fd
< fdp
->fd_freefile
)
119 fdp
->fd_freefile
= fd
;
121 if (fd
> fdp
->fd_lastfile
)
122 panic("fdrelse: fd_lastfile inconsistent");
124 fdp
->fd_ofiles
[fd
] = NULL
;
125 fdp
->fd_ofileflags
[fd
] = 0;
126 while ((fd
= fdp
->fd_lastfile
) > 0 &&
127 fdp
->fd_ofiles
[fd
] == NULL
&&
128 !(fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
133 * Duplicate a file descriptor.
142 struct dup_args
*uap
;
145 register struct filedesc
*fdp
= p
->p_fd
;
146 register int old
= uap
->fd
;
149 if ((u_int
)old
>= fdp
->fd_nfiles
||
150 fdp
->fd_ofiles
[old
] == NULL
||
151 (fdp
->fd_ofileflags
[old
] & UF_RESERVED
))
153 if (error
= fdalloc(p
, 0, &new))
155 return (finishdup(fdp
, old
, new, retval
));
159 * Duplicate a file descriptor to a particular value.
169 struct dup2_args
*uap
;
172 register struct filedesc
*fdp
= p
->p_fd
;
173 register int old
= uap
->from
, new = uap
->to
;
176 if ((u_int
)old
>= fdp
->fd_nfiles
||
177 fdp
->fd_ofiles
[old
] == NULL
||
178 (fdp
->fd_ofileflags
[old
] & UF_RESERVED
) ||
179 (u_int
)new >= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
180 (u_int
)new >= maxfiles
)
186 if ((u_int
)new >= fdp
->fd_nfiles
) {
187 if (error
= fdalloc(p
, new, &i
))
197 if ((flags
= fdp
->fd_ofileflags
[new]) & UF_RESERVED
)
199 fdp
->fd_ofileflags
[new] = (flags
& ~UF_MAPPED
) | UF_RESERVED
;
201 * dup2() must succeed even if the close has an error.
203 if (*(fpp
= &fdp
->fd_ofiles
[new])) {
204 struct file
*fp
= *fpp
;
207 (void) closef(fp
, p
);
210 return (finishdup(fdp
, old
, new, retval
));
214 * The file control system call.
223 fcntl(p
, uap
, retval
)
225 register struct fcntl_args
*uap
;
229 register struct filedesc
*fdp
= p
->p_fd
;
230 register struct file
*fp
;
232 struct vnode
*vp
, *devvp
;
233 int i
, tmp
, error
, error2
, flg
= F_POSIX
;
235 fstore_t alloc_struct
; /* structure for allocate command */
236 u_int32_t alloc_flags
= 0;
237 off_t offset
; /* used for F_SETSIZE */
239 struct radvisory ra_struct
;
240 fbootstraptransfer_t fbt_struct
; /* for F_READBOOTSTRAP and F_WRITEBOOTSTRAP */
241 struct log2phys l2p_struct
; /* structure for allocate command */
243 int devBlockSize
= 0;
245 if ((u_int
)fd
>= fdp
->fd_nfiles
||
246 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
247 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
249 pop
= &fdp
->fd_ofileflags
[fd
];
253 newmin
= (long)uap
->arg
;
254 if ((u_int
)newmin
>= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
255 (u_int
)newmin
>= maxfiles
)
257 if (error
= fdalloc(p
, newmin
, &i
))
259 return (finishdup(fdp
, fd
, i
, retval
));
262 *retval
= (*pop
& UF_EXCLOSE
)? 1 : 0;
266 *pop
= (*pop
&~ UF_EXCLOSE
) |
267 ((long)(uap
->arg
) & 1)? UF_EXCLOSE
: 0;
271 *retval
= OFLAGS(fp
->f_flag
);
275 fp
->f_flag
&= ~FCNTLFLAGS
;
276 fp
->f_flag
|= FFLAGS((long)uap
->arg
) & FCNTLFLAGS
;
277 tmp
= fp
->f_flag
& FNONBLOCK
;
278 error
= (*fp
->f_ops
->fo_ioctl
)(fp
, FIONBIO
, (caddr_t
)&tmp
, p
);
281 tmp
= fp
->f_flag
& FASYNC
;
282 error
= (*fp
->f_ops
->fo_ioctl
)(fp
, FIOASYNC
, (caddr_t
)&tmp
, p
);
285 fp
->f_flag
&= ~FNONBLOCK
;
287 (void) (*fp
->f_ops
->fo_ioctl
)(fp
, FIONBIO
, (caddr_t
)&tmp
, p
);
291 if (fp
->f_type
== DTYPE_SOCKET
) {
292 *retval
= ((struct socket
*)fp
->f_data
)->so_pgid
;
295 error
= (*fp
->f_ops
->fo_ioctl
)
296 (fp
, (int)TIOCGPGRP
, (caddr_t
)retval
, p
);
301 if (fp
->f_type
== DTYPE_SOCKET
) {
302 ((struct socket
*)fp
->f_data
)->so_pgid
=
306 if ((long)uap
->arg
<= 0) {
307 uap
->arg
= (void *)(-(long)(uap
->arg
));
309 struct proc
*p1
= pfind((long)uap
->arg
);
312 uap
->arg
= (void *)(long)p1
->p_pgrp
->pg_id
;
314 return ((*fp
->f_ops
->fo_ioctl
)
315 (fp
, (int)TIOCSPGRP
, (caddr_t
)&uap
->arg
, p
));
319 /* Fall into F_SETLK */
322 if (fp
->f_type
!= DTYPE_VNODE
)
324 vp
= (struct vnode
*)fp
->f_data
;
325 /* Copy in the lock structure */
326 error
= copyin((caddr_t
)uap
->arg
, (caddr_t
)&fl
,
330 if (fl
.l_whence
== SEEK_CUR
)
331 fl
.l_start
+= fp
->f_offset
;
335 if ((fp
->f_flag
& FREAD
) == 0)
337 p
->p_flag
|= P_ADVLOCK
;
338 return (VOP_ADVLOCK(vp
, (caddr_t
)p
, F_SETLK
, &fl
, flg
));
341 if ((fp
->f_flag
& FWRITE
) == 0)
343 p
->p_flag
|= P_ADVLOCK
;
344 return (VOP_ADVLOCK(vp
, (caddr_t
)p
, F_SETLK
, &fl
, flg
));
347 return (VOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &fl
,
355 if (fp
->f_type
!= DTYPE_VNODE
)
357 vp
= (struct vnode
*)fp
->f_data
;
358 /* Copy in the lock structure */
359 error
= copyin((caddr_t
)uap
->arg
, (caddr_t
)&fl
,
363 if (fl
.l_whence
== SEEK_CUR
)
364 fl
.l_start
+= fp
->f_offset
;
365 if (error
= VOP_ADVLOCK(vp
, (caddr_t
)p
, F_GETLK
, &fl
, F_POSIX
))
367 return (copyout((caddr_t
)&fl
, (caddr_t
)uap
->arg
,
372 /* Copy in the structure */
374 error
= copyin((caddr_t
)uap
->arg
, (caddr_t
)&alloc_struct
,
375 sizeof (alloc_struct
));
380 /* now set the space allocated to 0 and pass it out in
381 case we get a parameter checking error */
383 alloc_struct
.fst_bytesalloc
= 0;
385 error
= copyout((caddr_t
)&alloc_struct
, (caddr_t
)uap
->arg
,
386 sizeof (alloc_struct
));
391 /* First make sure that we have write permission */
393 if ((fp
->f_flag
& FWRITE
) == 0)
397 /* Do some simple parameter checking */
400 /* set up the flags */
402 alloc_flags
|= PREALLOCATE
;
404 if (alloc_struct
.fst_flags
& F_ALLOCATECONTIG
) {
405 alloc_flags
|= ALLOCATECONTIG
;
408 if (alloc_struct
.fst_flags
& F_ALLOCATEALL
) {
409 alloc_flags
|= ALLOCATEALL
;
412 /* Do any position mode specific stuff. The only */
413 /* position mode supported now is PEOFPOSMODE */
415 switch (alloc_struct
.fst_posmode
) {
419 if (alloc_struct
.fst_offset
!= 0)
422 alloc_flags
|= ALLOCATEFROMPEOF
;
427 if (alloc_struct
.fst_offset
<= 0)
430 alloc_flags
|= ALLOCATEFROMVOL
;
439 /* Now lock the vnode and call allocate to get the space */
441 vp
= (struct vnode
*)fp
->f_data
;
443 VOP_LOCK(vp
,LK_EXCLUSIVE
,p
);
444 error
= VOP_ALLOCATE(vp
,alloc_struct
.fst_length
,alloc_flags
,
445 &alloc_struct
.fst_bytesalloc
, alloc_struct
.fst_offset
,
449 if (error2
= (copyout((caddr_t
)&alloc_struct
, (caddr_t
)uap
->arg
,
450 sizeof (alloc_struct
)))) {
462 /* Copy in the structure */
464 error
= copyin((caddr_t
)uap
->arg
, (caddr_t
)&offset
,
471 /* First make sure that we are root. Growing a file */
472 /* without zero filling the data is a security hole */
473 /* root would have access anyway so we'll allow it */
479 /* Now lock the vnode and call allocate to get the space */
481 vp
= (struct vnode
*)fp
->f_data
;
483 VOP_LOCK(vp
,LK_EXCLUSIVE
,p
);
484 error
= VOP_TRUNCATE(vp
,offset
,IO_NOZEROFILL
,fp
->f_cred
,p
);
490 vp
= (struct vnode
*)fp
->f_data
;
492 simple_lock(&vp
->v_interlock
);
494 vp
->v_flag
&= ~VRAOFF
;
496 vp
->v_flag
|= VRAOFF
;
497 simple_unlock(&vp
->v_interlock
);
502 vp
= (struct vnode
*)fp
->f_data
;
504 simple_lock(&vp
->v_interlock
);
506 vp
->v_flag
|= VNOCACHE_DATA
;
508 vp
->v_flag
&= ~VNOCACHE_DATA
;
509 simple_unlock(&vp
->v_interlock
);
514 vp
= (struct vnode
*)fp
->f_data
;
516 if (error
= copyin((caddr_t
)uap
->arg
, (caddr_t
)&ra_struct
, sizeof (ra_struct
)))
518 return (VOP_IOCTL(vp
, 1, &ra_struct
, 0, fp
->f_cred
, p
));
520 case F_READBOOTSTRAP
:
521 case F_WRITEBOOTSTRAP
:
523 /* Copy in the structure */
525 error
= copyin((caddr_t
)uap
->arg
, (caddr_t
)&fbt_struct
,
526 sizeof (fbt_struct
));
532 if (uap
->cmd
== F_WRITEBOOTSTRAP
) {
533 /* First make sure that we are root. Updating the */
534 /* bootstrap on a disk could be a security hole */
541 /* Now lock the vnode and call VOP_IOCTL to handle the I/O: */
543 vp
= (struct vnode
*)fp
->f_data
;
544 if (vp
->v_tag
!= VT_HFS
) {
547 VOP_LOCK(vp
,LK_EXCLUSIVE
,p
);
548 error
= VOP_IOCTL(vp
, (uap
->cmd
== F_WRITEBOOTSTRAP
) ? 3 : 2, &fbt_struct
, 0, fp
->f_cred
, p
);
555 if (fp
->f_type
!= DTYPE_VNODE
)
557 vp
= (struct vnode
*)fp
->f_data
;
558 VOP_LOCK(vp
, LK_EXCLUSIVE
, p
);
559 if (VOP_OFFTOBLK(vp
, fp
->f_offset
, &lbn
))
560 panic("fcntl LOG2PHYS OFFTOBLK");
561 if (VOP_BLKTOOFF(vp
, lbn
, &offset
))
562 panic("fcntl LOG2PHYS BLKTOOFF1");
563 error
= VOP_BMAP(vp
, lbn
, &devvp
, &bn
, 0);
564 VOP_DEVBLOCKSIZE(devvp
, &devBlockSize
);
565 VOP_UNLOCK(vp
, 0, p
);
567 l2p_struct
.l2p_flags
= 0; /* for now */
568 l2p_struct
.l2p_contigbytes
= 0; /* for now */
569 l2p_struct
.l2p_devoffset
= bn
* devBlockSize
;
570 l2p_struct
.l2p_devoffset
+= fp
->f_offset
- offset
;
571 error
= copyout((caddr_t
)&l2p_struct
,
573 sizeof (l2p_struct
));
585 * Common code for dup, dup2, and fcntl(F_DUPFD).
588 finishdup(fdp
, old
, new, retval
)
589 register struct filedesc
*fdp
;
590 register int old
, new;
593 register struct file
*fp
;
595 if ((fp
= fdp
->fd_ofiles
[old
]) == NULL
||
596 (fdp
->fd_ofileflags
[old
] & UF_RESERVED
)) {
600 fdp
->fd_ofiles
[new] = fp
;
601 fdp
->fd_ofileflags
[new] = fdp
->fd_ofileflags
[old
] &~ UF_EXCLOSE
;
603 if (new > fdp
->fd_lastfile
)
604 fdp
->fd_lastfile
= new;
610 * Close a file descriptor.
617 close(p
, uap
, retval
)
619 struct close_args
*uap
;
623 register struct filedesc
*fdp
= p
->p_fd
;
624 register struct file
*fp
;
626 if ((u_int
)fd
>= fdp
->fd_nfiles
||
627 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
628 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
631 return (closef(fp
, p
));
635 * Return status information about a file descriptor.
643 fstat(p
, uap
, retval
)
645 register struct fstat_args
*uap
;
649 register struct filedesc
*fdp
= p
->p_fd
;
650 register struct file
*fp
;
654 if ((u_int
)fd
>= fdp
->fd_nfiles
||
655 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
656 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
658 switch (fp
->f_type
) {
661 error
= vn_stat((struct vnode
*)fp
->f_data
, &ub
, p
);
665 error
= soo_stat((struct socket
*)fp
->f_data
, &ub
);
669 error
= pshm_stat((void *)fp
->f_data
, &ub
);
676 error
= copyout((caddr_t
)&ub
, (caddr_t
)uap
->sb
,
683 * Return status information about a file descriptor.
690 ofstat(p
, uap
, retval
)
692 register struct ofstat_args
*uap
;
696 register struct filedesc
*fdp
= p
->p_fd
;
697 register struct file
*fp
;
702 if ((u_int
)fd
>= fdp
->fd_nfiles
||
703 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
704 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
706 switch (fp
->f_type
) {
709 error
= vn_stat((struct vnode
*)fp
->f_data
, &ub
, p
);
713 error
= soo_stat((struct socket
*)fp
->f_data
, &ub
);
722 error
= copyout((caddr_t
)&oub
, (caddr_t
)uap
->sb
,
726 #endif /* COMPAT_43 */
729 * Return pathconf information about a file descriptor.
731 struct fpathconf_args
{
736 fpathconf(p
, uap
, retval
)
738 register struct fpathconf_args
*uap
;
742 struct filedesc
*fdp
= p
->p_fd
;
746 if ((u_int
)fd
>= fdp
->fd_nfiles
||
747 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
748 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
750 switch (fp
->f_type
) {
753 if (uap
->name
!= _PC_PIPE_BUF
)
759 vp
= (struct vnode
*)fp
->f_data
;
760 return (VOP_PATHCONF(vp
, uap
->name
, retval
));
769 * Allocate a file descriptor for the process.
774 fdalloc(p
, want
, result
)
779 register struct filedesc
*fdp
= p
->p_fd
;
781 int lim
, last
, nfiles
, oldnfiles
;
782 struct file
**newofiles
, **ofiles
;
783 char *newofileflags
, *ofileflags
;
786 * Search for a free descriptor starting at the higher
787 * of want or fd_freefile. If that fails, consider
788 * expanding the ofile array.
790 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
792 last
= min(fdp
->fd_nfiles
, lim
);
793 if ((i
= want
) < fdp
->fd_freefile
)
794 i
= fdp
->fd_freefile
;
795 ofiles
= &fdp
->fd_ofiles
[i
];
796 ofileflags
= &fdp
->fd_ofileflags
[i
];
797 for (; i
< last
; i
++) {
798 if (*ofiles
== NULL
&& !(*ofileflags
& UF_RESERVED
)) {
799 *ofileflags
= UF_RESERVED
;
800 if (i
> fdp
->fd_lastfile
)
801 fdp
->fd_lastfile
= i
;
802 if (want
<= fdp
->fd_freefile
)
803 fdp
->fd_freefile
= i
;
807 ofiles
++; ofileflags
++;
811 * No space in current array. Expand?
813 if (fdp
->fd_nfiles
>= lim
)
815 if (fdp
->fd_nfiles
< NDEXTENT
)
818 nfiles
= 2 * fdp
->fd_nfiles
;
822 MALLOC_ZONE(newofiles
, struct file
**,
823 nfiles
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
824 if (fdp
->fd_nfiles
>= nfiles
) {
825 FREE_ZONE(newofiles
, nfiles
* OFILESIZE
, M_OFILETABL
);
828 newofileflags
= (char *) &newofiles
[nfiles
];
830 * Copy the existing ofile and ofileflags arrays
831 * and zero the new portion of each array.
833 oldnfiles
= fdp
->fd_nfiles
;
834 (void) memcpy(newofiles
, fdp
->fd_ofiles
,
835 oldnfiles
* sizeof *fdp
->fd_ofiles
);
836 (void) memset(&newofiles
[oldnfiles
], 0,
837 (nfiles
- oldnfiles
) * sizeof *fdp
->fd_ofiles
);
839 (void) memcpy(newofileflags
, fdp
->fd_ofileflags
,
840 oldnfiles
* sizeof *fdp
->fd_ofileflags
);
841 (void) memset(&newofileflags
[oldnfiles
], 0,
842 (nfiles
- oldnfiles
) *
843 sizeof *fdp
->fd_ofileflags
);
844 ofiles
= fdp
->fd_ofiles
;
845 fdp
->fd_ofiles
= newofiles
;
846 fdp
->fd_ofileflags
= newofileflags
;
847 fdp
->fd_nfiles
= nfiles
;
848 FREE_ZONE(ofiles
, oldnfiles
* OFILESIZE
, M_OFILETABL
);
854 * Check to see whether n user file descriptors
855 * are available to the process p.
862 register struct filedesc
*fdp
= p
->p_fd
;
863 register struct file
**fpp
;
864 register char *flags
;
867 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
868 if ((i
= lim
- fdp
->fd_nfiles
) > 0 && (n
-= i
) <= 0)
870 fpp
= &fdp
->fd_ofiles
[fdp
->fd_freefile
];
871 flags
= &fdp
->fd_ofileflags
[fdp
->fd_freefile
];
872 for (i
= fdp
->fd_nfiles
- fdp
->fd_freefile
; --i
>= 0; fpp
++, flags
++)
873 if (*fpp
== NULL
&& !(*flags
& UF_RESERVED
) && --n
<= 0)
883 _fdrelse(p
->p_fd
, fd
);
887 fdgetf(p
, fd
, resultfp
)
888 register struct proc
*p
;
890 struct file
**resultfp
;
892 register struct filedesc
*fdp
= p
->p_fd
;
895 if ((u_int
)fd
>= fdp
->fd_nfiles
||
896 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
897 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
906 * Create a new open file structure and allocate
907 * a file decriptor for the process that refers to it.
910 falloc(p
, resultfp
, resultfd
)
911 register struct proc
*p
;
912 struct file
**resultfp
;
915 register struct file
*fp
, *fq
;
918 if (error
= fdalloc(p
, 0, &i
))
920 if (nfiles
>= maxfiles
) {
925 * Allocate a new file descriptor.
926 * If the process has file descriptor zero open, add to the list
927 * of open files at that point, otherwise put it at the front of
928 * the list of open files.
931 MALLOC_ZONE(fp
, struct file
*, sizeof(struct file
), M_FILE
, M_WAITOK
);
932 bzero(fp
, sizeof(struct file
));
933 if (fq
= p
->p_fd
->fd_ofiles
[0]) {
934 LIST_INSERT_AFTER(fq
, fp
, f_list
);
936 LIST_INSERT_HEAD(&filehead
, fp
, f_list
);
938 p
->p_fd
->fd_ofiles
[i
] = fp
;
940 fp
->f_cred
= p
->p_ucred
;
950 * Free a file structure.
954 register struct file
*fp
;
956 register struct file
*fq
;
959 LIST_REMOVE(fp
, f_list
);
961 if (cred
!= NOCRED
) {
969 FREE_ZONE(fp
, sizeof *fp
, M_FILE
);
976 register struct filedesc
*fdp
= p
->p_fd
;
977 register int i
= fdp
->fd_lastfile
;
978 register struct file
**fpp
= &fdp
->fd_ofiles
[i
];
979 register char *flags
= &fdp
->fd_ofileflags
[i
];
982 if ((*flags
& (UF_RESERVED
|UF_EXCLOSE
)) == UF_EXCLOSE
) {
983 register struct file
*fp
= *fpp
;
985 *fpp
= NULL
; *flags
= 0;
986 if (i
== fdp
->fd_lastfile
&& i
> 0)
991 *flags
&= ~UF_MAPPED
;
998 * Copy a filedesc structure.
1004 register struct filedesc
*newfdp
, *fdp
= p
->p_fd
;
1007 MALLOC_ZONE(newfdp
, struct filedesc
*,
1008 sizeof *newfdp
, M_FILEDESC
, M_WAITOK
);
1009 (void) memcpy(newfdp
, fdp
, sizeof *newfdp
);
1010 VREF(newfdp
->fd_cdir
);
1011 if (newfdp
->fd_rdir
)
1012 VREF(newfdp
->fd_rdir
);
1013 newfdp
->fd_refcnt
= 1;
1016 * If the number of open files fits in the internal arrays
1017 * of the open file structure, use them, otherwise allocate
1018 * additional memory for the number of descriptors currently
1021 if (newfdp
->fd_lastfile
< NDFILE
)
1025 * Compute the smallest multiple of NDEXTENT needed
1026 * for the file descriptors currently in use,
1027 * allowing the table to shrink.
1029 i
= newfdp
->fd_nfiles
;
1030 while (i
> 2 * NDEXTENT
&& i
> newfdp
->fd_lastfile
* 2)
1033 MALLOC_ZONE(newfdp
->fd_ofiles
, struct file
**,
1034 i
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
1035 newfdp
->fd_ofileflags
= (char *) &newfdp
->fd_ofiles
[i
];
1036 newfdp
->fd_nfiles
= i
;
1037 if (fdp
->fd_nfiles
> 0) {
1038 register struct file
**fpp
;
1039 register char *flags
;
1041 (void) memcpy(newfdp
->fd_ofiles
, fdp
->fd_ofiles
,
1042 i
* sizeof *fdp
->fd_ofiles
);
1043 (void) memcpy(newfdp
->fd_ofileflags
, fdp
->fd_ofileflags
,
1044 i
* sizeof *fdp
->fd_ofileflags
);
1046 fpp
= newfdp
->fd_ofiles
;
1047 flags
= newfdp
->fd_ofileflags
;
1048 for (i
= newfdp
->fd_lastfile
; i
-- >= 0; fpp
++, flags
++)
1049 if (*fpp
!= NULL
&& !(*flags
& UF_RESERVED
)) {
1056 (void) memset(newfdp
->fd_ofiles
, 0, i
* OFILESIZE
);
1062 * Release a filedesc structure.
1068 struct filedesc
*fdp
;
1073 if ((fdp
= p
->p_fd
) == NULL
)
1075 if (--fdp
->fd_refcnt
> 0)
1078 if (fdp
->fd_nfiles
> 0) {
1079 fpp
= fdp
->fd_ofiles
;
1080 for (i
= fdp
->fd_lastfile
; i
-- >= 0; fpp
++)
1082 (void) closef(*fpp
, p
);
1083 FREE_ZONE(fdp
->fd_ofiles
,
1084 fdp
->fd_nfiles
* OFILESIZE
, M_OFILETABL
);
1087 fdp
->fd_cdir
= NULL
;
1091 fdp
->fd_rdir
= NULL
;
1094 FREE_ZONE(fdp
, sizeof *fdp
, M_FILEDESC
);
1098 * Internal form of close.
1099 * Decrement reference count on file structure.
1100 * Note: p may be NULL when closing a file
1101 * that was being passed in a message.
1105 register struct file
*fp
;
1106 register struct proc
*p
;
1115 * POSIX record locking dictates that any close releases ALL
1116 * locks owned by this process. This is handled by setting
1117 * a flag in the unlock to free ONLY locks obeying POSIX
1118 * semantics, and not to free BSD-style file locks.
1119 * If the descriptor was in a message, POSIX-style locks
1120 * aren't passed with the descriptor.
1122 if (p
&& (p
->p_flag
& P_ADVLOCK
) && fp
->f_type
== DTYPE_VNODE
) {
1123 lf
.l_whence
= SEEK_SET
;
1126 lf
.l_type
= F_UNLCK
;
1127 vp
= (struct vnode
*)fp
->f_data
;
1128 (void) VOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &lf
, F_POSIX
);
1132 if ((fp
->f_flag
& FHASLOCK
) && fp
->f_type
== DTYPE_VNODE
) {
1133 lf
.l_whence
= SEEK_SET
;
1136 lf
.l_type
= F_UNLCK
;
1137 vp
= (struct vnode
*)fp
->f_data
;
1138 (void) VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_UNLCK
, &lf
, F_FLOCK
);
1141 error
= (*fp
->f_ops
->fo_close
)(fp
, p
);
1149 * Apply an advisory lock on a file descriptor.
1151 * Just attempt to get a record lock of the requested type on
1152 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
1160 flock(p
, uap
, retval
)
1162 register struct flock_args
*uap
;
1167 register struct filedesc
*fdp
= p
->p_fd
;
1168 register struct file
*fp
;
1172 if ((u_int
)fd
>= fdp
->fd_nfiles
||
1173 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
1174 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
1176 if (fp
->f_type
!= DTYPE_VNODE
)
1177 return (EOPNOTSUPP
);
1178 vp
= (struct vnode
*)fp
->f_data
;
1179 lf
.l_whence
= SEEK_SET
;
1182 if (how
& LOCK_UN
) {
1183 lf
.l_type
= F_UNLCK
;
1184 fp
->f_flag
&= ~FHASLOCK
;
1185 return (VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_UNLCK
, &lf
, F_FLOCK
));
1188 lf
.l_type
= F_WRLCK
;
1189 else if (how
& LOCK_SH
)
1190 lf
.l_type
= F_RDLCK
;
1193 fp
->f_flag
|= FHASLOCK
;
1195 return (VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_SETLK
, &lf
, F_FLOCK
));
1196 return (VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_SETLK
, &lf
, F_FLOCK
|F_WAIT
));
1200 * File Descriptor pseudo-device driver (/dev/fd/).
1202 * Opening minor device N dup()s the file (if any) connected to file
1203 * descriptor N belonging to the calling process. Note that this driver
1204 * consists of only the ``open()'' routine, because all subsequent
1205 * references to this file will be direct to the other driver.
1209 fdopen(dev
, mode
, type
, p
)
1216 * XXX Kludge: set curproc->p_dupfd to contain the value of the
1217 * the file descriptor being sought for duplication. The error
1218 * return ensures that the vnode for this device will be released
1219 * by vn_open. Open will detect this special error and take the
1220 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
1221 * will simply report the error.
1223 p
->p_dupfd
= minor(dev
);
1228 * Duplicate the specified descriptor to a free descriptor.
1231 dupfdopen(fdp
, indx
, dfd
, mode
, error
)
1232 register struct filedesc
*fdp
;
1233 register int indx
, dfd
;
1237 register struct file
*wfp
;
1241 * If the to-be-dup'd fd number is greater than the allowed number
1242 * of file descriptors, or the fd to be dup'd has already been
1243 * closed, reject. Note, check for new == old is necessary as
1244 * falloc could allocate an already closed to-be-dup'd descriptor
1245 * as the new descriptor.
1247 fp
= fdp
->fd_ofiles
[indx
];
1248 if ((u_int
)dfd
>= fdp
->fd_nfiles
||
1249 (wfp
= fdp
->fd_ofiles
[dfd
]) == NULL
|| wfp
== fp
||
1250 (fdp
->fd_ofileflags
[dfd
] & UF_RESERVED
))
1254 * There are two cases of interest here.
1256 * For ENODEV simply dup (dfd) to file descriptor
1257 * (indx) and return.
1259 * For ENXIO steal away the file structure from (dfd) and
1260 * store it in (indx). (dfd) is effectively closed by
1263 * Any other error code is just returned.
1268 * Check that the mode the file is being opened for is a
1269 * subset of the mode of the existing descriptor.
1271 if (((mode
& (FREAD
|FWRITE
)) | wfp
->f_flag
) != wfp
->f_flag
)
1274 if (indx
> fdp
->fd_lastfile
)
1275 fdp
->fd_lastfile
= indx
;;
1276 fdp
->fd_ofiles
[indx
] = wfp
;
1277 fdp
->fd_ofileflags
[indx
] = fdp
->fd_ofileflags
[dfd
];
1282 * Steal away the file pointer from dfd, and stuff it into indx.
1284 if (indx
> fdp
->fd_lastfile
)
1285 fdp
->fd_lastfile
= indx
;;
1286 fdp
->fd_ofiles
[indx
] = fdp
->fd_ofiles
[dfd
];
1287 fdp
->fd_ofileflags
[indx
] = fdp
->fd_ofileflags
[dfd
];
1297 /* Reference manipulation routines for the file structure */
1300 fref(struct file
*fp
)
1302 if (++fp
->f_count
<= 0)
1303 panic("fref: f_count");
1304 return ((int)fp
->f_count
);
1308 frele(struct file
*fp
)
1310 if (--fp
->f_count
< 0)
1311 panic("frele: count < 0");
1312 return ((int)fp
->f_count
);
1316 fcount(struct file
*fp
)
1318 return ((int)fp
->f_count
);