xref: /dragonfly/sys/sys/filedesc.h (revision 6e285212)
1 /*
2  * Copyright (c) 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)filedesc.h	8.1 (Berkeley) 6/2/93
34  * $FreeBSD: src/sys/sys/filedesc.h,v 1.19.2.5 2003/06/06 20:21:32 tegge Exp $
35  * $DragonFly: src/sys/sys/filedesc.h,v 1.2 2003/06/17 04:28:58 dillon Exp $
36  */
37 
38 #ifndef _SYS_FILEDESC_H_
39 #define _SYS_FILEDESC_H_
40 
41 #include <sys/queue.h>
42 
43 /*
44  * This structure is used for the management of descriptors.  It may be
45  * shared by multiple processes.
46  *
47  * A process is initially started out with NDFILE descriptors stored within
48  * this structure, selected to be enough for typical applications based on
49  * the historical limit of 20 open files (and the usage of descriptors by
50  * shells).  If these descriptors are exhausted, a larger descriptor table
51  * may be allocated, up to a process' resource limit; the internal arrays
52  * are then unused.  The initial expansion is set to NDEXTENT; each time
53  * it runs out, it is doubled until the resource limit is reached. NDEXTENT
54  * should be selected to be the biggest multiple of OFILESIZE (see below)
55  * that will fit in a power-of-two sized piece of memory.
56  */
57 #define NDFILE		20
58 #define NDEXTENT	50		/* 250 bytes in 256-byte alloc. */
59 
60 struct klist;
61 
62 struct filedesc {
63 	struct	file **fd_ofiles;	/* file structures for open files */
64 	char	*fd_ofileflags;		/* per-process open file flags */
65 	struct	vnode *fd_cdir;		/* current directory */
66 	struct	vnode *fd_rdir;		/* root directory */
67 	struct	vnode *fd_jdir;		/* jail root directory */
68 	int	fd_nfiles;		/* number of open files allocated */
69 	u_short	fd_lastfile;		/* high-water mark of fd_ofiles */
70 	u_short	fd_freefile;		/* approx. next free file */
71 	u_short	fd_cmask;		/* mask for file creation */
72 	u_short	fd_refcnt;		/* reference count */
73 
74 	int	fd_knlistsize;		/* size of knlist */
75 	struct	klist *fd_knlist;	/* list of attached knotes */
76 	u_long	fd_knhashmask;		/* size of knhash */
77 	struct	klist *fd_knhash;	/* hash table for attached knotes */
78 	int	fd_holdleaderscount;	/* block fdfree() for shared close() */
79 	int	fd_holdleaderswakeup;	/* fdfree() needs wakeup */
80 };
81 
82 /*
83  * Basic allocation of descriptors:
84  * one of the above, plus arrays for NDFILE descriptors.
85  */
86 struct filedesc0 {
87 	struct	filedesc fd_fd;
88 	/*
89 	 * These arrays are used when the number of open files is
90 	 * <= NDFILE, and are then pointed to by the pointers above.
91 	 */
92 	struct	file *fd_dfiles[NDFILE];
93 	char	fd_dfileflags[NDFILE];
94 };
95 
96 
97 
98 /*
99  * Structure to keep track of (process leader, struct fildedesc) tuples.
100  * Each process has a pointer to such a structure when detailed tracking
101  * is needed. e.g. when rfork(RFPROC | RFMEM) causes a file descriptor
102  * table to be shared by processes having different "p_leader" pointers
103  * and thus distinct POSIX style locks.
104  */
105 struct filedesc_to_leader {
106 	int		fdl_refcount;	/* references from struct proc */
107 	int		fdl_holdcount;	/* temporary hold during closef */
108 	int		fdl_wakeup;	/* fdfree() waits on closef() */
109 	struct proc	*fdl_leader;	/* owner of POSIX locks */
110 	/* Circular list */
111 	struct filedesc_to_leader *fdl_prev;
112 	struct filedesc_to_leader *fdl_next;
113 };
114 
115 /*
116  * Per-process open flags.
117  */
118 #define	UF_EXCLOSE 	0x01		/* auto-close on exec */
119 #if 0
120 #define	UF_MAPPED 	0x02		/* mapped from device */
121 #endif
122 
123 /*
124  * Storage required per open file descriptor.
125  */
126 #define OFILESIZE (sizeof(struct file *) + sizeof(char))
127 
128 /*
129  * This structure that holds the information needed to send a SIGIO or
130  * a SIGURG signal to a process or process group when new data arrives
131  * on a device or socket.  The structure is placed on an SLIST belonging
132  * to the proc or pgrp so that the entire list may be revoked when the
133  * process exits or the process group disappears.
134  */
135 struct	sigio {
136 	union {
137 		struct	proc *siu_proc; /* process to receive SIGIO/SIGURG */
138 		struct	pgrp *siu_pgrp; /* process group to receive ... */
139 	} sio_u;
140 	SLIST_ENTRY(sigio) sio_pgsigio;	/* sigio's for process or group */
141 	struct	sigio **sio_myref;	/* location of the pointer that holds
142 					 * the reference to this structure */
143 	struct	ucred *sio_ucred;	/* current credentials */
144 	uid_t	sio_ruid;		/* real user id */
145 	pid_t	sio_pgid;		/* pgid for signals */
146 };
147 #define	sio_proc	sio_u.siu_proc
148 #define	sio_pgrp	sio_u.siu_pgrp
149 
150 SLIST_HEAD(sigiolst, sigio);
151 
152 #ifdef _KERNEL
153 /*
154  * Kernel global variables and routines.
155  */
156 int	dupfdopen __P((struct proc *, struct filedesc *, int, int, int, int));
157 int	fdalloc __P((struct proc *p, int want, int *result));
158 int	fdavail __P((struct proc *p, int n));
159 int	falloc __P((struct proc *p, struct file **resultfp, int *resultfd));
160 void	ffree __P((struct file *));
161 struct	filedesc *fdinit __P((struct proc *p));
162 struct	filedesc *fdshare __P((struct proc *p));
163 struct	filedesc *fdcopy __P((struct proc *p));
164 void	fdfree __P((struct proc *p));
165 int	closef __P((struct file *fp,struct proc *p));
166 void	fdcloseexec __P((struct proc *p));
167 int	fdcheckstd __P((struct proc *p));
168 struct	file *holdfp __P((struct filedesc* fdp, int fd, int flag));
169 int	getvnode __P((struct filedesc *fdp, int fd, struct file **fpp));
170 int	fdissequential __P((struct file *));
171 void	fdsequential __P((struct file *, int));
172 pid_t	fgetown __P((struct sigio *));
173 int	fsetown __P((pid_t, struct sigio **));
174 void	funsetown __P((struct sigio *));
175 void	funsetownlst __P((struct sigiolst *));
176 void	setugidsafety __P((struct proc *p));
177 
178 struct filedesc_to_leader *
179 filedesc_to_leader_alloc(struct filedesc_to_leader *old,
180 			 struct proc *leader);
181 
182 #endif
183 
184 #endif
185