| 1 | /*	$OpenBSD: filedesc.h,v 1.49 2026/03/08 16:41:19 deraadt Exp $	*/ |
| 2 | /*	$NetBSD: filedesc.h,v 1.14 1996/04/09 20:55:28 cgd Exp $	*/ |
| 3 | |
| 4 | /* |
| 5 | * Copyright (c) 1990, 1993 |
| 6 | *	The Regents of the University of California. All rights reserved. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in the |
| 15 | * documentation and/or other materials provided with the distribution. |
| 16 | * 3. Neither the name of the University nor the names of its contributors |
| 17 | * may be used to endorse or promote products derived from this software |
| 18 | * without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 30 | * SUCH DAMAGE. |
| 31 | * |
| 32 | *	@(#)filedesc.h	8.1 (Berkeley) 6/2/93 |
| 33 | */ |
| 34 | |
| 35 | #include <sys/mutex.h> |
| 36 | #include <sys/rwlock.h> |
| 37 | /* |
| 38 | * This structure is used for the management of descriptors. It may be |
| 39 | * shared by multiple processes. |
| 40 | * |
| 41 | * A process is initially started out with NDFILE descriptors stored within |
| 42 | * this structure, selected to be enough for typical applications based on |
| 43 | * the historical limit of 20 open files (and the usage of descriptors by |
| 44 | * shells). If these descriptors are exhausted, a larger descriptor table |
| 45 | * may be allocated, up to a process' resource limit; the internal arrays |
| 46 | * are then unused. The initial expansion is set to NDEXTENT; each time |
| 47 | * it runs out, it is doubled until the resource limit is reached. NDEXTENT |
| 48 | * should be selected to be the biggest multiple of OFILESIZE (see below) |
| 49 | * that will fit in a power-of-two sized piece of memory. |
| 50 | */ |
| 51 | #define NDFILE		20 |
| 52 | #define NDEXTENT	50		/* 250 bytes in 256-byte alloc. */ |
| 53 | #define NDENTRIES	32		/* 32 fds per entry */ |
| 54 | #define NDENTRYMASK	(NDENTRIES - 1) |
| 55 | #define NDENTRYSHIFT	5		/* bits per entry */ |
| 56 | #define NDREDUCE(x)	(((x) + NDENTRIES - 1) >> NDENTRYSHIFT) |
| 57 | #define NDHISLOTS(x)	(NDREDUCE(NDREDUCE(x))) |
| 58 | #define NDLOSLOTS(x)	(NDHISLOTS(x) << NDENTRYSHIFT) |
| 59 | |
| 60 | struct kqueue; |
| 61 | |
| 62 | /* |
| 63 | * Locking: |
| 64 | *	a	atomic operations |
| 65 | *	f	fd_lock |
| 66 | *	f/w	fd_lock when writing |
| 67 | *	K	kernel lock |
| 68 | *	m	fd_fplock |
| 69 | */ |
| 70 | struct filedesc { |
| 71 | 	struct	file **fd_ofiles;	/* [f/w,m] file structures for |
| 72 | 					 * open files */ |
| 73 | 	char	*fd_ofileflags;		/* [f] per-process open file flags */ |
| 74 | 	struct	vnode *fd_cdir;		/* [K] current directory */ |
| 75 | 	struct	vnode *fd_rdir;		/* [K] root directory */ |
| 76 | 	int	fd_nfiles;		/* [f] number of open files allocated */ |
| 77 | 	int	fd_openfd;		/* [f] number of files currently open */ |
| 78 | 	u_int	*fd_himap;		/* [f] each bit points to 32 fds */ |
| 79 | 	u_int	*fd_lomap;		/* [f] bitmap of free fds */ |
| 80 | 	int	fd_lastfile;		/* [f] high-water mark of fd_ofiles */ |
| 81 | 	int	fd_freefile;		/* [f] approx. next free file */ |
| 82 | 	mode_t	fd_cmask;		/* [f/w] mask for file creation */ |
| 83 | 	u_int	fd_refcnt;		/* [K] reference count */ |
| 84 | 	struct rwlock fd_lock;		/* lock for the file descs */ |
| 85 | 	struct mutex fd_fplock;		/* lock for reading fd_ofiles without |
| 86 | 					 * fd_lock */ |
| 87 | 	LIST_HEAD(, kqueue) fd_kqlist;	/* [f] kqueues attached to this |
| 88 | 					 * filedesc */ |
| 89 | 	int fd_flags;			/* [a] flags on this filedesc */ |
| 90 | 	u_int fd_nuserevents;		/* [a] number of kqueue user events */ |
| 91 | }; |
| 92 | |
| 93 | /* |
| 94 | * Basic allocation of descriptors: |
| 95 | * one of the above, plus arrays for NDFILE descriptors. |
| 96 | */ |
| 97 | struct filedesc0 { |
| 98 | 	struct	filedesc fd_fd; |
| 99 | 	/* |
| 100 | 	 * These arrays are used when the number of open files is |
| 101 | 	 * <= NDFILE, and are then pointed to by the pointers above. |
| 102 | 	 */ |
| 103 | 	struct	file *fd_dfiles[NDFILE]; |
| 104 | 	char	fd_dfileflags[NDFILE]; |
| 105 | 	/* |
| 106 | 	 * There arrays are used when the number of open files is |
| 107 | 	 * <= 1024, and are then pointed to by the pointers above. |
| 108 | 	 */ |
| 109 | 	u_int fd_dhimap[NDENTRIES >> NDENTRYSHIFT]; |
| 110 | 	u_int fd_dlomap[NDENTRIES]; |
| 111 | }; |
| 112 | |
| 113 | /* |
| 114 | * Per-process open flags. |
| 115 | */ |
| 116 | #define	UF_EXCLOSE 	0x01		/* auto-close on exec */ |
| 117 | #define	UF_PLEDGED 	0x02		/* opened after pledge(2) */ |
| 118 | #define	UF_FORKCLOSE 	0x04		/* auto-close on fork */ |
| 119 | #define	UF_PLEDGEOPEN 	0x08		/* opened with __pledge_open() */ |
| 120 | |
| 121 | /* |
| 122 | * Flags on the file descriptor table. |
| 123 | */ |
| 124 | #define FD_ADVLOCK	0x01		/* May hold a POSIX adv. lock. */ |
| 125 | |
| 126 | /* |
| 127 | * Storage required per open file descriptor. |
| 128 | */ |
| 129 | #define OFILESIZE (sizeof(struct file *) + sizeof(char)) |
| 130 | |
| 131 | #ifdef _KERNEL |
| 132 | /* |
| 133 | * Kernel global variables and routines. |
| 134 | */ |
| 135 | void	filedesc_init(void); |
| 136 | int	dupfdopen(struct proc *, int, int); |
| 137 | int	fdalloc(struct proc *p, int want, int *result); |
| 138 | void	fdexpand(struct proc *); |
| 139 | struct	file *fnew(struct proc *_p); |
| 140 | int	falloc(struct proc *_p, struct file **_rfp, int *_rfd); |
| 141 | struct	filedesc *fdinit(void); |
| 142 | struct	filedesc *fdshare(struct process *); |
| 143 | struct	filedesc *fdcopy(struct process *); |
| 144 | void	fdfree(struct proc *p); |
| 145 | int	fdrelease(struct proc *p, int); |
| 146 | void	fdinsert(struct filedesc *, int, int, struct file *); |
| 147 | void	fdremove(struct filedesc *, int); |
| 148 | void	fdprepforexec(struct proc *); |
| 149 | struct file *fd_iterfile(struct file *, struct proc *); |
| 150 | struct file *fd_getfile(struct filedesc *, int); |
| 151 | struct file *fd_getfile_mode(struct filedesc *, int, int); |
| 152 | int	fd_checkclosed(struct filedesc *, int, struct file *); |
| 153 | |
| 154 | int	closef(struct file *, struct proc *); |
| 155 | int	getsock(struct proc *, int, struct file **); |
| 156 | |
| 157 | #define	fdplock(fdp)	do { NET_ASSERT_UNLOCKED(); rw_enter_write(&(fdp)->fd_lock); } while (0) |
| 158 | #define	fdpunlock(fdp)	rw_exit_write(&(fdp)->fd_lock) |
| 159 | #define	fdpassertlocked(fdp)	rw_assert_wrlock(&(fdp)->fd_lock) |
| 160 | #endif |