xref: /netbsd-src/share/man/man9/filedesc.9 (revision 482a367e19b07394750279e08dc4131d0a947c47)
1*482a367eSwiz.\"     $NetBSD: filedesc.9,v 1.18 2019/04/08 13:30:46 wiz Exp $
2174f011eSgmcgarry.\"
3ff8e3c8cSrpaulo.\" Copyright (c) 2002, 2005, 2006 The NetBSD Foundation, Inc.
4174f011eSgmcgarry.\" All rights reserved.
5174f011eSgmcgarry.\"
6174f011eSgmcgarry.\" This code is derived from software contributed to The NetBSD Foundation
7174f011eSgmcgarry.\" by Gregory McGarry.
8174f011eSgmcgarry.\"
9174f011eSgmcgarry.\" Redistribution and use in source and binary forms, with or without
10174f011eSgmcgarry.\" modification, are permitted provided that the following conditions
11174f011eSgmcgarry.\" are met:
12174f011eSgmcgarry.\" 1. Redistributions of source code must retain the above copyright
13174f011eSgmcgarry.\"    notice, this list of conditions and the following disclaimer.
14174f011eSgmcgarry.\" 2. Redistributions in binary form must reproduce the above copyright
15174f011eSgmcgarry.\"    notice, this list of conditions and the following disclaimer in the
16174f011eSgmcgarry.\"    documentation and/or other materials provided with the distribution.
17174f011eSgmcgarry.\"
18174f011eSgmcgarry.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19174f011eSgmcgarry.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20174f011eSgmcgarry.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21174f011eSgmcgarry.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22174f011eSgmcgarry.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23174f011eSgmcgarry.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24174f011eSgmcgarry.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25174f011eSgmcgarry.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26174f011eSgmcgarry.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27174f011eSgmcgarry.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28174f011eSgmcgarry.\" POSSIBILITY OF SUCH DAMAGE.
29174f011eSgmcgarry.\"
30059f51c3Smaya.Dd April 8, 2019
31174f011eSgmcgarry.Dt FILEDESC 9
32174f011eSgmcgarry.Os
33174f011eSgmcgarry.Sh NAME
34174f011eSgmcgarry.Nm filedesc ,
35059f51c3Smaya.Nm fd_alloc ,
36059f51c3Smaya.Nm fd_checkstd ,
37059f51c3Smaya.Nm fd_clone ,
38059f51c3Smaya.Nm fd_closeexec ,
39059f51c3Smaya.Nm fd_copy ,
40059f51c3Smaya.Nm fd_dup ,
41059f51c3Smaya.Nm fd_dup2 ,
42059f51c3Smaya.Nm fd_dupopen ,
43059f51c3Smaya.Nm fd_free ,
44059f51c3Smaya.Nm fd_init ,
45174f011eSgmcgarry.Nm fd_getfile ,
46059f51c3Smaya.Nm fd_share ,
47*482a367eSwiz.Nm fd_tryexpand
48174f011eSgmcgarry.Nd file descriptor tables and operations
49174f011eSgmcgarry.Sh SYNOPSIS
50472351e1Swiz.In sys/file.h
51472351e1Swiz.In sys/filedesc.h
52174f011eSgmcgarry.Ft int
53059f51c3Smaya.Fn fd_alloc "proc_t *p" "int want" "int *result"
54174f011eSgmcgarry.Ft int
55059f51c3Smaya.Fn fd_checkstd "void"
56174f011eSgmcgarry.Ft int
57059f51c3Smaya.Fn fd_clone "file_t *fp" "int fd" "int flag" "const struct fileops *fops" "void *data"
58059f51c3Smaya.Ft filedesc_t *
59059f51c3Smaya.Fn fd_copy "void"
60059f51c3Smaya.Ft void
61059f51c3Smaya.Fn fd_closeexec "void"
62174f011eSgmcgarry.Ft int
63059f51c3Smaya.Fn fd_dup "file_t *fp" "int minfd" "int *newp" "bool exclose"
649b67fde0Spooka.Ft int
65059f51c3Smaya.Fn fd_dup2 "file_t *fp" "unsigned newfd" "int flags"
66174f011eSgmcgarry.Ft int
67059f51c3Smaya.Fn fd_dupopen "int old" "int *newp" "int error"
68174f011eSgmcgarry.Ft void
69059f51c3Smaya.Fn fd_free "void"
70059f51c3Smaya.Ft filedesc_t *
71059f51c3Smaya.Fn fd_init "filedesc_t *fdp"
72059f51c3Smaya.Ft file_t *
73059f51c3Smaya.Fn fd_getfile "unsigned fd"
74174f011eSgmcgarry.Ft void
75059f51c3Smaya.Fn fd_share "proc_t *p"
76174f011eSgmcgarry.Ft void
77059f51c3Smaya.Fn fd_tryexpand "proc_t *p"
78174f011eSgmcgarry.Sh DESCRIPTION
79152e83aaSwizFor user processes, all I/O is done through file descriptors.
80152e83aaSwizThese file descriptors represent underlying objects supported by the kernel
81152e83aaSwizand are created by system calls specific to the type of object.
82152e83aaSwizIn
83174f011eSgmcgarry.Nx ,
849b67fde0Spookasix types of objects can be represented by file descriptors: data
853cfbbb69Swizfiles, pipes, sockets, event queues, crypto, and miscellaneous.
86174f011eSgmcgarry.Pp
87174f011eSgmcgarryThe kernel maintains a descriptor table for each process which is used
88174f011eSgmcgarryto translate the external representation of a file descriptor into an
89152e83aaSwizinternal representation.
90152e83aaSwizThe file descriptor is merely an index into this table.
91059f51c3SmayaThe table maintains the following information:
92174f011eSgmcgarry.Pp
93174f011eSgmcgarry.Bl -bullet -compact
94174f011eSgmcgarry.It
95174f011eSgmcgarrythe number of descriptors allocated in the file descriptor table;
96174f011eSgmcgarry.It
97174f011eSgmcgarryapproximate next free descriptor;
98174f011eSgmcgarry.It
99174f011eSgmcgarrya reference count on the file descriptor table; and
100174f011eSgmcgarry.It
101174f011eSgmcgarryan array of open file entries.
102174f011eSgmcgarry.El
103174f011eSgmcgarry.Pp
104174f011eSgmcgarryOn creation of the file descriptor table, a fixed number of file
105152e83aaSwizentries are created.
106152e83aaSwizIt is the responsibility of the file descriptor operations to expand the
107152e83aaSwizavailable number of entries if more are required.
108059f51c3SmayaEach file entry in the descriptor table contains the information needed
109152e83aaSwizto access the underlying object and to maintain common information.
110152e83aaSwizSee
111174f011eSgmcgarry.Xr file 9
112174f011eSgmcgarryfor details of operations on the file entries.
113174f011eSgmcgarry.Pp
114174f011eSgmcgarryNew file descriptors are generally allocated by
115059f51c3Smaya.Fn fd_alloc
116174f011eSgmcgarryand freed by
117059f51c3Smaya.Fn fd_free .
118174f011eSgmcgarryFile entries are extracted from the file descriptor table by
119174f011eSgmcgarry.Fn fd_getfile .
120059f51c3SmayaMost of the remaining functions in the interface are purpose-specific
121174f011eSgmcgarryand perform lower-level file descriptor operations.
122174f011eSgmcgarry.Sh FUNCTIONS
123174f011eSgmcgarryThe following functions are high-level interface routines to access
124174f011eSgmcgarrythe file descriptor table for a process and its file entries.
125174f011eSgmcgarry.Bl -tag -width compact
126059f51c3Smaya.It Fn fd_alloc "p" "want" "*result"
127059f51c3SmayaCreate a new open file entry in the file descriptor table and
128059f51c3Smayaallocate a file descriptor for the process
1298d8283f5Srpaulo.Fa p .
130c5926dd0SkleinkThe credential on the file entry are inherited from process
131c5926dd0Skleink.Fa p .
132*482a367eSwizCalling the
133059f51c3Smaya.Fn fd_alloc
134*482a367eSwizfunction expands the file descriptor table when necessary.
135174f011eSgmcgarry.Pp
136059f51c3SmayaThe index of the file entry is returned in
137059f51c3Smaya.Fa *result .
138174f011eSgmcgarryThe
139059f51c3Smaya.Fn fd_alloc
140059f51c3Smayafunction returns zero on success, or an appropriate error value
141059f51c3Smayaotherwise.
142059f51c3Smaya.It Fn fd_getfile "fd"
143059f51c3SmayaGet the file entry for file descriptor .
144174f011eSgmcgarry.Fa fd
145059f51c3SmayaThe file entry is returned if it is valid and usable, otherwise
146152e83aaSwiz.Dv NULL
147174f011eSgmcgarryis returned.
148059f51c3Smaya.It Fn fd_dup "fp" "minfd" "*newp" "exclose"
149174f011eSgmcgarryDuplicate file descriptor
150059f51c3Smaya.Fa fp
151059f51c3Smayafor the current process.
152059f51c3SmayaThe fd picked will be at least
153059f51c3Smaya.Fa minfd .
154059f51c3SmayaThe resulting descriptor is given in
155059f51c3Smaya.Fa newp .
156059f51c3Smaya.It Fn fd_dup2 "fp" "newfd" "flags"
157059f51c3SmayaDuplicate file descriptor
158059f51c3Smaya.Fa fp
159059f51c3Smayain fd number
160059f51c3Smaya.Fa newfd .
161059f51c3SmayaIf newfd is already in use by an open file, close it (See
162059f51c3Smaya.Xr dup2 2 ) .
163059f51c3Smaya.It Fn fd_dupopen "old" "*newp" "error"
164059f51c3SmayaDuplicate file descriptor specified in
165059f51c3Smaya.Fa old
166059f51c3Smayafor the current lwp.
167174f011eSgmcgarry.El
168174f011eSgmcgarry.Pp
169174f011eSgmcgarryThe following functions operate on the file descriptor table for a
170174f011eSgmcgarryprocess.
171174f011eSgmcgarry.Bl -tag -width compact
172059f51c3Smaya.It Fn fd_alloc "p" "want" "*result"
173174f011eSgmcgarryAllocate a file descriptor
174174f011eSgmcgarry.Fa want
175c5926dd0Skleinkfor process
176c5926dd0Skleink.Fa p .
177174f011eSgmcgarryThe resultant file descriptor is returned in
178174f011eSgmcgarry.Fa *result .
179174f011eSgmcgarryThe
180059f51c3Smaya.Fn fd_alloc
181174f011eSgmcgarryfunction returns zero on success, otherwise an appropriate error is
182174f011eSgmcgarryreturned.
183059f51c3Smaya.It Fn fd_clone "fp" "fd" "flag" "fops" "data"
1849b67fde0SpookaThis function is meant to be used by devices which allocate a file
1859b67fde0Spookaentry upon open.
186059f51c3Smaya.Fn fd_clone
1879b67fde0Spookafills
1889b67fde0Spooka.Fa fp
1899b67fde0Spookawith the given parameters.
190059f51c3SmayaIt always returns the in-kernel errno
191059f51c3Smaya.Er EMOVEFD .
1929b67fde0SpookaThis special return value is interpreted by the caller of the device
1939b67fde0Spookaopen routine.
194059f51c3Smaya.It Fn fd_closeexec "void"
195059f51c3SmayaClose any files for the current process
196152e83aaSwizthat are marked
197152e83aaSwiz.Dq close on exec .
198152e83aaSwizThis operation is performed by invoking
199059f51c3Smaya.Fn fd_close
200174f011eSgmcgarryon the appropriate file descriptor.
201059f51c3Smaya.It Fn fd_copy "void"
202059f51c3SmayaCopy the file descriptor table from the current process
203152e83aaSwizand return a pointer to the copy.
204152e83aaSwizThe returned file descriptor is guaranteed to have a reference count of one.
205152e83aaSwizAll file descriptor state is maintained.
206152e83aaSwizThe reference counts on each file entry referenced by the file
207152e83aaSwizdescriptor table is incremented accordingly.
208059f51c3Smaya.It Fn fd_tryexpand "p"
209c5926dd0SkleinkExpand the file descriptor table for process
210c5926dd0Skleink.Fa p
211174f011eSgmcgarryby allocating memory for additional file descriptors.
212059f51c3Smaya.It Fn fd_free "void"
213059f51c3SmayaDecrement the reference count on the file descriptor table for the current lwp
214174f011eSgmcgarryand release the file descriptor table if the reference count drops to
215174f011eSgmcgarryzero.
216059f51c3Smaya.It Fn fd_share "p"
217059f51c3SmayaMake process
218059f51c3Smaya.Fa p
219059f51c3Smayashare the current process's filedesc structure.
220059f51c3Smaya.It Fn fd_checkstd "l"
221059f51c3SmayaCheck the standard file descriptors 0, 1, and 2 and ensure they are
222059f51c3Smayareferencing valid file descriptors.
223059f51c3SmayaIf they are not, create references to
224059f51c3Smaya.Pa /dev/null .
225059f51c3SmayaThis is done to setuid/setgid executables, as file descriptors 0, 1, 2
226059f51c3Smayaare implicitly used by the Standard C Library.
227059f51c3Smaya.It Fn fd_init "fdp"
228174f011eSgmcgarryCreate a file descriptor table using the same current and root
229059f51c3Smayadirectories of the current process.
230174f011eSgmcgarryThe returned file descriptor table is guaranteed to have a reference
231174f011eSgmcgarrycount of one.
232174f011eSgmcgarry.El
233174f011eSgmcgarry.Sh RETURN VALUES
234152e83aaSwizSuccessful operations return zero.
235059f51c3SmayaA failed operation will return a non-zero value.
236152e83aaSwizPossible values include:
237174f011eSgmcgarry.Bl -tag -width Er
238174f011eSgmcgarry.It Bq Er EBADF
239174f011eSgmcgarryBad file descriptor specified.
240174f011eSgmcgarry.It Bq Er EMFILE
241174f011eSgmcgarryCannot exceed file descriptor limit.
242174f011eSgmcgarry.It Bq Er ENOSPC
243174f011eSgmcgarryNo space left in file descriptor table.
244174f011eSgmcgarry.El
245174f011eSgmcgarry.Sh CODE REFERENCES
246174f011eSgmcgarryThe framework for file descriptor handling is implemented within the
247174f011eSgmcgarryfile
248174f011eSgmcgarry.Pa sys/kern/kern_descrip.c .
249174f011eSgmcgarry.Sh SEE ALSO
250174f011eSgmcgarry.Xr file 9
251