xref: /netbsd/sys/sys/filedesc.h (revision c4a72b64)
1 /*	$NetBSD: filedesc.h,v 1.25 2002/10/23 09:14:58 jdolecek Exp $	*/
2 
3 /*
4  * Copyright (c) 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)filedesc.h	8.1 (Berkeley) 6/2/93
36  */
37 
38 #ifndef _SYS_FILEDESC_H_
39 #define	_SYS_FILEDESC_H_
40 
41 /*
42  * This structure is used for the management of descriptors.  It may be
43  * shared by multiple processes.
44  *
45  * A process is initially started out with NDFILE descriptors stored within
46  * this structure, selected to be enough for typical applications based on
47  * the historical limit of 20 open files (and the usage of descriptors by
48  * shells).  If these descriptors are exhausted, a larger descriptor table
49  * may be allocated, up to a process' resource limit; the internal arrays
50  * are then unused.  The initial expansion is set to NDEXTENT; each time
51  * it runs out, it is doubled until the resource limit is reached. NDEXTENT
52  * should be selected to be the biggest multiple of OFILESIZE (see below)
53  * that will fit in a power-of-two sized piece of memory.
54  */
55 #define	NDFILE		20
56 #define	NDEXTENT	50		/* 250 bytes in 256-byte alloc */
57 
58 struct filedesc {
59 	struct file	**fd_ofiles;	/* file structures for open files */
60 	char		*fd_ofileflags;	/* per-process open file flags */
61 	int		fd_nfiles;	/* number of open files allocated */
62 	int		fd_lastfile;	/* high-water mark of fd_ofiles */
63 	int		fd_freefile;	/* approx. next free file */
64 	int		fd_refcnt;	/* reference count */
65 
66 	int		fd_knlistsize;	/* size of fd_knlist */
67 	struct klist	*fd_knlist;	/*
68 					 * list of attached fd knotes,
69 					 * indexed by fd number
70 					 */
71 	u_long		fd_knhashmask;	/* size of fd_knhash */
72 	struct klist	*fd_knhash;	/*
73 					 * hash table for attached
74 					 * non-fd knotes
75 					 */
76 };
77 
78 struct cwdinfo {
79 	struct vnode	*cwdi_cdir;	/* current directory */
80 	struct vnode	*cwdi_rdir;	/* root directory */
81 	u_short		cwdi_cmask;	/* mask for file creation */
82 	u_short		cwdi_refcnt;	/* reference count */
83 };
84 
85 
86 /*
87  * Basic allocation of descriptors:
88  * one of the above, plus arrays for NDFILE descriptors.
89  */
90 struct filedesc0 {
91 	struct filedesc	fd_fd;
92 	/*
93 	 * These arrays are used when the number of open files is
94 	 * <= NDFILE, and are then pointed to by the pointers above.
95 	 */
96 	struct file	*fd_dfiles[NDFILE];
97 	char		fd_dfileflags[NDFILE];
98 };
99 
100 /*
101  * Per-process open flags.
102  */
103 #define	UF_EXCLOSE 	0x01		/* auto-close on exec */
104 
105 /*
106  * Storage required per open file descriptor.
107  */
108 #define	OFILESIZE (sizeof(struct file *) + sizeof(char))
109 
110 #ifdef _KERNEL
111 /*
112  * Kernel global variables and routines.
113  */
114 int	dupfdopen(struct proc *p, int indx, int dfd, int mode, int error);
115 int	fdalloc(struct proc *p, int want, int *result);
116 void	fdexpand(struct proc *p);
117 int	fdavail(struct proc *p, int n);
118 int	falloc(struct proc *p, struct file **resultfp, int *resultfd);
119 void	ffree(struct file *);
120 struct filedesc *fdcopy(struct proc *p);
121 struct filedesc *fdinit(struct proc *p);
122 void	fdshare(struct proc *p1, struct proc *p2);
123 void	fdunshare(struct proc *p);
124 void	fdinit1(struct filedesc0 *newfdp);
125 void	fdclear(struct proc *p);
126 void	fdfree(struct proc *p);
127 void	fdremove(struct filedesc *, int);
128 int	fdrelease(struct proc *, int);
129 void	fdcloseexec(struct proc *);
130 int	fdcheckstd(struct proc *);
131 
132 struct file *fd_getfile(struct filedesc *, int);
133 
134 struct cwdinfo *cwdinit(struct proc *);
135 void	cwdshare(struct proc *, struct proc *);
136 void	cwdunshare(struct proc *);
137 void	cwdfree(struct proc *);
138 
139 int	closef(struct file *, struct proc *);
140 int	getsock(struct filedesc *, int, struct file **);
141 #endif /* _KERNEL */
142 
143 #endif /* !_SYS_FILEDESC_H_ */
144