xref: /f-stack/freebsd/sys/filedesc.h (revision a9643ea8)
1*a9643ea8Slogwang /*-
2*a9643ea8Slogwang  * Copyright (c) 1990, 1993
3*a9643ea8Slogwang  *	The Regents of the University of California.  All rights reserved.
4*a9643ea8Slogwang  *
5*a9643ea8Slogwang  * Redistribution and use in source and binary forms, with or without
6*a9643ea8Slogwang  * modification, are permitted provided that the following conditions
7*a9643ea8Slogwang  * are met:
8*a9643ea8Slogwang  * 1. Redistributions of source code must retain the above copyright
9*a9643ea8Slogwang  *    notice, this list of conditions and the following disclaimer.
10*a9643ea8Slogwang  * 2. Redistributions in binary form must reproduce the above copyright
11*a9643ea8Slogwang  *    notice, this list of conditions and the following disclaimer in the
12*a9643ea8Slogwang  *    documentation and/or other materials provided with the distribution.
13*a9643ea8Slogwang  * 4. Neither the name of the University nor the names of its contributors
14*a9643ea8Slogwang  *    may be used to endorse or promote products derived from this software
15*a9643ea8Slogwang  *    without specific prior written permission.
16*a9643ea8Slogwang  *
17*a9643ea8Slogwang  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18*a9643ea8Slogwang  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*a9643ea8Slogwang  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*a9643ea8Slogwang  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21*a9643ea8Slogwang  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*a9643ea8Slogwang  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*a9643ea8Slogwang  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*a9643ea8Slogwang  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*a9643ea8Slogwang  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*a9643ea8Slogwang  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*a9643ea8Slogwang  * SUCH DAMAGE.
28*a9643ea8Slogwang  *
29*a9643ea8Slogwang  *	@(#)filedesc.h	8.1 (Berkeley) 6/2/93
30*a9643ea8Slogwang  * $FreeBSD$
31*a9643ea8Slogwang  */
32*a9643ea8Slogwang 
33*a9643ea8Slogwang #ifndef _SYS_FILEDESC_H_
34*a9643ea8Slogwang #define	_SYS_FILEDESC_H_
35*a9643ea8Slogwang 
36*a9643ea8Slogwang #include <sys/caprights.h>
37*a9643ea8Slogwang #include <sys/queue.h>
38*a9643ea8Slogwang #include <sys/event.h>
39*a9643ea8Slogwang #include <sys/lock.h>
40*a9643ea8Slogwang #include <sys/priority.h>
41*a9643ea8Slogwang #include <sys/seq.h>
42*a9643ea8Slogwang #include <sys/sx.h>
43*a9643ea8Slogwang 
44*a9643ea8Slogwang #include <machine/_limits.h>
45*a9643ea8Slogwang 
46*a9643ea8Slogwang struct filecaps {
47*a9643ea8Slogwang 	cap_rights_t	 fc_rights;	/* per-descriptor capability rights */
48*a9643ea8Slogwang 	u_long		*fc_ioctls;	/* per-descriptor allowed ioctls */
49*a9643ea8Slogwang 	int16_t		 fc_nioctls;	/* fc_ioctls array size */
50*a9643ea8Slogwang 	uint32_t	 fc_fcntls;	/* per-descriptor allowed fcntls */
51*a9643ea8Slogwang };
52*a9643ea8Slogwang 
53*a9643ea8Slogwang struct filedescent {
54*a9643ea8Slogwang 	struct file	*fde_file;	/* file structure for open file */
55*a9643ea8Slogwang 	struct filecaps	 fde_caps;	/* per-descriptor rights */
56*a9643ea8Slogwang 	uint8_t		 fde_flags;	/* per-process open file flags */
57*a9643ea8Slogwang 	seq_t		 fde_seq;	/* keep file and caps in sync */
58*a9643ea8Slogwang };
59*a9643ea8Slogwang #define	fde_rights	fde_caps.fc_rights
60*a9643ea8Slogwang #define	fde_fcntls	fde_caps.fc_fcntls
61*a9643ea8Slogwang #define	fde_ioctls	fde_caps.fc_ioctls
62*a9643ea8Slogwang #define	fde_nioctls	fde_caps.fc_nioctls
63*a9643ea8Slogwang #define	fde_change_size	(offsetof(struct filedescent, fde_seq))
64*a9643ea8Slogwang 
65*a9643ea8Slogwang struct fdescenttbl {
66*a9643ea8Slogwang 	int	fdt_nfiles;		/* number of open files allocated */
67*a9643ea8Slogwang 	struct	filedescent fdt_ofiles[0];	/* open files */
68*a9643ea8Slogwang };
69*a9643ea8Slogwang #define	fd_seq(fdt, fd)	(&(fdt)->fdt_ofiles[(fd)].fde_seq)
70*a9643ea8Slogwang 
71*a9643ea8Slogwang /*
72*a9643ea8Slogwang  * This structure is used for the management of descriptors.  It may be
73*a9643ea8Slogwang  * shared by multiple processes.
74*a9643ea8Slogwang  */
75*a9643ea8Slogwang #define NDSLOTTYPE	u_long
76*a9643ea8Slogwang 
77*a9643ea8Slogwang struct filedesc {
78*a9643ea8Slogwang 	struct	fdescenttbl *fd_files;	/* open files table */
79*a9643ea8Slogwang 	struct	vnode *fd_cdir;		/* current directory */
80*a9643ea8Slogwang 	struct	vnode *fd_rdir;		/* root directory */
81*a9643ea8Slogwang 	struct	vnode *fd_jdir;		/* jail root directory */
82*a9643ea8Slogwang 	NDSLOTTYPE *fd_map;		/* bitmap of free fds */
83*a9643ea8Slogwang 	int	fd_lastfile;		/* high-water mark of fd_ofiles */
84*a9643ea8Slogwang 	int	fd_freefile;		/* approx. next free file */
85*a9643ea8Slogwang 	u_short	fd_cmask;		/* mask for file creation */
86*a9643ea8Slogwang 	int	fd_refcnt;		/* thread reference count */
87*a9643ea8Slogwang 	int	fd_holdcnt;		/* hold count on structure + mutex */
88*a9643ea8Slogwang 	struct	sx fd_sx;		/* protects members of this struct */
89*a9643ea8Slogwang 	struct	kqlist fd_kqlist;	/* list of kqueues on this filedesc */
90*a9643ea8Slogwang 	int	fd_holdleaderscount;	/* block fdfree() for shared close() */
91*a9643ea8Slogwang 	int	fd_holdleaderswakeup;	/* fdfree() needs wakeup */
92*a9643ea8Slogwang };
93*a9643ea8Slogwang 
94*a9643ea8Slogwang /*
95*a9643ea8Slogwang  * Structure to keep track of (process leader, struct fildedesc) tuples.
96*a9643ea8Slogwang  * Each process has a pointer to such a structure when detailed tracking
97*a9643ea8Slogwang  * is needed, e.g., when rfork(RFPROC | RFMEM) causes a file descriptor
98*a9643ea8Slogwang  * table to be shared by processes having different "p_leader" pointers
99*a9643ea8Slogwang  * and thus distinct POSIX style locks.
100*a9643ea8Slogwang  *
101*a9643ea8Slogwang  * fdl_refcount and fdl_holdcount are protected by struct filedesc mtx.
102*a9643ea8Slogwang  */
103*a9643ea8Slogwang struct filedesc_to_leader {
104*a9643ea8Slogwang 	int		fdl_refcount;	/* references from struct proc */
105*a9643ea8Slogwang 	int		fdl_holdcount;	/* temporary hold during closef */
106*a9643ea8Slogwang 	int		fdl_wakeup;	/* fdfree() waits on closef() */
107*a9643ea8Slogwang 	struct proc	*fdl_leader;	/* owner of POSIX locks */
108*a9643ea8Slogwang 	/* Circular list: */
109*a9643ea8Slogwang 	struct filedesc_to_leader *fdl_prev;
110*a9643ea8Slogwang 	struct filedesc_to_leader *fdl_next;
111*a9643ea8Slogwang };
112*a9643ea8Slogwang #define	fd_nfiles	fd_files->fdt_nfiles
113*a9643ea8Slogwang #define	fd_ofiles	fd_files->fdt_ofiles
114*a9643ea8Slogwang 
115*a9643ea8Slogwang /*
116*a9643ea8Slogwang  * Per-process open flags.
117*a9643ea8Slogwang  */
118*a9643ea8Slogwang #define	UF_EXCLOSE	0x01		/* auto-close on exec */
119*a9643ea8Slogwang 
120*a9643ea8Slogwang #ifdef _KERNEL
121*a9643ea8Slogwang 
122*a9643ea8Slogwang /* Lock a file descriptor table. */
123*a9643ea8Slogwang #define	FILEDESC_LOCK_INIT(fdp)	sx_init(&(fdp)->fd_sx, "filedesc structure")
124*a9643ea8Slogwang #define	FILEDESC_LOCK_DESTROY(fdp)	sx_destroy(&(fdp)->fd_sx)
125*a9643ea8Slogwang #define	FILEDESC_LOCK(fdp)	(&(fdp)->fd_sx)
126*a9643ea8Slogwang #define	FILEDESC_XLOCK(fdp)	sx_xlock(&(fdp)->fd_sx)
127*a9643ea8Slogwang #define	FILEDESC_XUNLOCK(fdp)	sx_xunlock(&(fdp)->fd_sx)
128*a9643ea8Slogwang #define	FILEDESC_SLOCK(fdp)	sx_slock(&(fdp)->fd_sx)
129*a9643ea8Slogwang #define	FILEDESC_SUNLOCK(fdp)	sx_sunlock(&(fdp)->fd_sx)
130*a9643ea8Slogwang 
131*a9643ea8Slogwang #define	FILEDESC_LOCK_ASSERT(fdp)	sx_assert(&(fdp)->fd_sx, SX_LOCKED | \
132*a9643ea8Slogwang 					    SX_NOTRECURSED)
133*a9643ea8Slogwang #define	FILEDESC_XLOCK_ASSERT(fdp)	sx_assert(&(fdp)->fd_sx, SX_XLOCKED | \
134*a9643ea8Slogwang 					    SX_NOTRECURSED)
135*a9643ea8Slogwang #define	FILEDESC_UNLOCK_ASSERT(fdp)	sx_assert(&(fdp)->fd_sx, SX_UNLOCKED)
136*a9643ea8Slogwang 
137*a9643ea8Slogwang /* Operation types for kern_dup(). */
138*a9643ea8Slogwang enum {
139*a9643ea8Slogwang 	FDDUP_NORMAL,		/* dup() behavior. */
140*a9643ea8Slogwang 	FDDUP_FCNTL,		/* fcntl()-style errors. */
141*a9643ea8Slogwang 	FDDUP_FIXED,		/* Force fixed allocation. */
142*a9643ea8Slogwang 	FDDUP_MUSTREPLACE,	/* Target must exist. */
143*a9643ea8Slogwang 	FDDUP_LASTMODE,
144*a9643ea8Slogwang };
145*a9643ea8Slogwang 
146*a9643ea8Slogwang /* Flags for kern_dup(). */
147*a9643ea8Slogwang #define	FDDUP_FLAG_CLOEXEC	0x1	/* Atomically set UF_EXCLOSE. */
148*a9643ea8Slogwang 
149*a9643ea8Slogwang /* For backward compatibility. */
150*a9643ea8Slogwang #define	falloc(td, resultfp, resultfd, flags) \
151*a9643ea8Slogwang 	falloc_caps(td, resultfp, resultfd, flags, NULL)
152*a9643ea8Slogwang 
153*a9643ea8Slogwang struct thread;
154*a9643ea8Slogwang 
155*a9643ea8Slogwang void	filecaps_init(struct filecaps *fcaps);
156*a9643ea8Slogwang int	filecaps_copy(const struct filecaps *src, struct filecaps *dst,
157*a9643ea8Slogwang 	    bool locked);
158*a9643ea8Slogwang void	filecaps_move(struct filecaps *src, struct filecaps *dst);
159*a9643ea8Slogwang void	filecaps_free(struct filecaps *fcaps);
160*a9643ea8Slogwang 
161*a9643ea8Slogwang int	closef(struct file *fp, struct thread *td);
162*a9643ea8Slogwang int	dupfdopen(struct thread *td, struct filedesc *fdp, int dfd, int mode,
163*a9643ea8Slogwang 	    int openerror, int *indxp);
164*a9643ea8Slogwang int	falloc_caps(struct thread *td, struct file **resultfp, int *resultfd,
165*a9643ea8Slogwang 	    int flags, struct filecaps *fcaps);
166*a9643ea8Slogwang int	falloc_noinstall(struct thread *td, struct file **resultfp);
167*a9643ea8Slogwang void	_finstall(struct filedesc *fdp, struct file *fp, int fd, int flags,
168*a9643ea8Slogwang 	    struct filecaps *fcaps);
169*a9643ea8Slogwang int	finstall(struct thread *td, struct file *fp, int *resultfd, int flags,
170*a9643ea8Slogwang 	    struct filecaps *fcaps);
171*a9643ea8Slogwang int	fdalloc(struct thread *td, int minfd, int *result);
172*a9643ea8Slogwang int	fdallocn(struct thread *td, int minfd, int *fds, int n);
173*a9643ea8Slogwang int	fdcheckstd(struct thread *td);
174*a9643ea8Slogwang void	fdclose(struct thread *td, struct file *fp, int idx);
175*a9643ea8Slogwang void	fdcloseexec(struct thread *td);
176*a9643ea8Slogwang void	fdsetugidsafety(struct thread *td);
177*a9643ea8Slogwang struct	filedesc *fdcopy(struct filedesc *fdp);
178*a9643ea8Slogwang int	fdcopy_remapped(struct filedesc *fdp, const int *fds, size_t nfds,
179*a9643ea8Slogwang 	    struct filedesc **newfdp);
180*a9643ea8Slogwang void	fdinstall_remapped(struct thread *td, struct filedesc *fdp);
181*a9643ea8Slogwang void	fdunshare(struct thread *td);
182*a9643ea8Slogwang void	fdescfree(struct thread *td);
183*a9643ea8Slogwang void	fdescfree_remapped(struct filedesc *fdp);
184*a9643ea8Slogwang struct	filedesc *fdinit(struct filedesc *fdp, bool prepfiles);
185*a9643ea8Slogwang struct	filedesc *fdshare(struct filedesc *fdp);
186*a9643ea8Slogwang struct filedesc_to_leader *
187*a9643ea8Slogwang 	filedesc_to_leader_alloc(struct filedesc_to_leader *old,
188*a9643ea8Slogwang 	    struct filedesc *fdp, struct proc *leader);
189*a9643ea8Slogwang int	getvnode(struct thread *td, int fd, cap_rights_t *rightsp,
190*a9643ea8Slogwang 	    struct file **fpp);
191*a9643ea8Slogwang void	mountcheckdirs(struct vnode *olddp, struct vnode *newdp);
192*a9643ea8Slogwang 
193*a9643ea8Slogwang /* Return a referenced file from an unlocked descriptor. */
194*a9643ea8Slogwang int	fget_unlocked(struct filedesc *fdp, int fd, cap_rights_t *needrightsp,
195*a9643ea8Slogwang 	    struct file **fpp, seq_t *seqp);
196*a9643ea8Slogwang 
197*a9643ea8Slogwang /* Requires a FILEDESC_{S,X}LOCK held and returns without a ref. */
198*a9643ea8Slogwang static __inline struct file *
199*a9643ea8Slogwang fget_locked(struct filedesc *fdp, int fd)
200*a9643ea8Slogwang {
201*a9643ea8Slogwang 
202*a9643ea8Slogwang 	FILEDESC_LOCK_ASSERT(fdp);
203*a9643ea8Slogwang 
204*a9643ea8Slogwang 	if (fd < 0 || fd > fdp->fd_lastfile)
205*a9643ea8Slogwang 		return (NULL);
206*a9643ea8Slogwang 
207*a9643ea8Slogwang 	return (fdp->fd_ofiles[fd].fde_file);
208*a9643ea8Slogwang }
209*a9643ea8Slogwang 
210*a9643ea8Slogwang static __inline bool
211*a9643ea8Slogwang fd_modified(struct filedesc *fdp, int fd, seq_t seq)
212*a9643ea8Slogwang {
213*a9643ea8Slogwang 
214*a9643ea8Slogwang 	return (!seq_consistent(fd_seq(fdp->fd_files, fd), seq));
215*a9643ea8Slogwang }
216*a9643ea8Slogwang 
217*a9643ea8Slogwang /* cdir/rdir/jdir manipulation functions. */
218*a9643ea8Slogwang void	pwd_chdir(struct thread *td, struct vnode *vp);
219*a9643ea8Slogwang int	pwd_chroot(struct thread *td, struct vnode *vp);
220*a9643ea8Slogwang void	pwd_ensure_dirs(void);
221*a9643ea8Slogwang 
222*a9643ea8Slogwang #endif /* _KERNEL */
223*a9643ea8Slogwang 
224*a9643ea8Slogwang #endif /* !_SYS_FILEDESC_H_ */
225