2019-04-08 16:30:46 +03:00
|
|
|
.\" $NetBSD: filedesc.9,v 1.18 2019/04/08 13:30:46 wiz Exp $
|
2002-10-20 09:16:31 +04:00
|
|
|
.\"
|
2006-01-29 06:09:19 +03:00
|
|
|
.\" Copyright (c) 2002, 2005, 2006 The NetBSD Foundation, Inc.
|
2002-10-20 09:16:31 +04:00
|
|
|
.\" All rights reserved.
|
|
|
|
.\"
|
|
|
|
.\" This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
.\" by Gregory McGarry.
|
|
|
|
.\"
|
|
|
|
.\" Redistribution and use in source and binary forms, with or without
|
|
|
|
.\" modification, are permitted provided that the following conditions
|
|
|
|
.\" are met:
|
|
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer.
|
|
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer in the
|
|
|
|
.\" documentation and/or other materials provided with the distribution.
|
|
|
|
.\"
|
|
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
2002-10-20 14:46:57 +04:00
|
|
|
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
2002-10-20 09:16:31 +04:00
|
|
|
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
.\" POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
.\"
|
2019-04-08 16:04:54 +03:00
|
|
|
.Dd April 8, 2019
|
2002-10-20 09:16:31 +04:00
|
|
|
.Dt FILEDESC 9
|
|
|
|
.Os
|
|
|
|
.Sh NAME
|
|
|
|
.Nm filedesc ,
|
2019-04-08 16:04:54 +03:00
|
|
|
.Nm fd_alloc ,
|
|
|
|
.Nm fd_checkstd ,
|
|
|
|
.Nm fd_clone ,
|
|
|
|
.Nm fd_closeexec ,
|
|
|
|
.Nm fd_copy ,
|
|
|
|
.Nm fd_dup ,
|
|
|
|
.Nm fd_dup2 ,
|
|
|
|
.Nm fd_dupopen ,
|
|
|
|
.Nm fd_free ,
|
|
|
|
.Nm fd_init ,
|
2002-10-20 09:16:31 +04:00
|
|
|
.Nm fd_getfile ,
|
2019-04-08 16:04:54 +03:00
|
|
|
.Nm fd_share ,
|
2019-04-08 16:30:46 +03:00
|
|
|
.Nm fd_tryexpand
|
2002-10-20 09:16:31 +04:00
|
|
|
.Nd file descriptor tables and operations
|
|
|
|
.Sh SYNOPSIS
|
2003-04-16 17:34:34 +04:00
|
|
|
.In sys/file.h
|
|
|
|
.In sys/filedesc.h
|
2002-10-20 09:16:31 +04:00
|
|
|
.Ft int
|
2019-04-08 16:04:54 +03:00
|
|
|
.Fn fd_alloc "proc_t *p" "int want" "int *result"
|
2002-10-20 09:16:31 +04:00
|
|
|
.Ft int
|
2019-04-08 16:04:54 +03:00
|
|
|
.Fn fd_checkstd "void"
|
2002-10-20 09:16:31 +04:00
|
|
|
.Ft int
|
2019-04-08 16:04:54 +03:00
|
|
|
.Fn fd_clone "file_t *fp" "int fd" "int flag" "const struct fileops *fops" "void *data"
|
|
|
|
.Ft filedesc_t *
|
|
|
|
.Fn fd_copy "void"
|
2002-10-20 09:16:31 +04:00
|
|
|
.Ft void
|
2019-04-08 16:04:54 +03:00
|
|
|
.Fn fd_closeexec "void"
|
2006-02-24 15:47:18 +03:00
|
|
|
.Ft int
|
2019-04-08 16:04:54 +03:00
|
|
|
.Fn fd_dup "file_t *fp" "int minfd" "int *newp" "bool exclose"
|
2002-10-20 09:16:31 +04:00
|
|
|
.Ft int
|
2019-04-08 16:04:54 +03:00
|
|
|
.Fn fd_dup2 "file_t *fp" "unsigned newfd" "int flags"
|
|
|
|
.Ft int
|
|
|
|
.Fn fd_dupopen "int old" "int *newp" "int error"
|
2002-10-20 09:16:31 +04:00
|
|
|
.Ft void
|
2019-04-08 16:04:54 +03:00
|
|
|
.Fn fd_free "void"
|
|
|
|
.Ft filedesc_t *
|
|
|
|
.Fn fd_init "filedesc_t *fdp"
|
|
|
|
.Ft file_t *
|
|
|
|
.Fn fd_getfile "unsigned fd"
|
2002-10-20 09:16:31 +04:00
|
|
|
.Ft void
|
2019-04-08 16:04:54 +03:00
|
|
|
.Fn fd_share "proc_t *p"
|
2002-10-20 09:16:31 +04:00
|
|
|
.Ft void
|
2019-04-08 16:04:54 +03:00
|
|
|
.Fn fd_tryexpand "proc_t *p"
|
2002-10-20 09:16:31 +04:00
|
|
|
.Sh DESCRIPTION
|
2002-10-20 15:04:52 +04:00
|
|
|
For user processes, all I/O is done through file descriptors.
|
|
|
|
These file descriptors represent underlying objects supported by the kernel
|
|
|
|
and are created by system calls specific to the type of object.
|
|
|
|
In
|
2002-10-20 09:16:31 +04:00
|
|
|
.Nx ,
|
2006-02-24 15:47:18 +03:00
|
|
|
six types of objects can be represented by file descriptors: data
|
2006-02-25 01:04:15 +03:00
|
|
|
files, pipes, sockets, event queues, crypto, and miscellaneous.
|
2002-10-20 09:16:31 +04:00
|
|
|
.Pp
|
|
|
|
The kernel maintains a descriptor table for each process which is used
|
|
|
|
to translate the external representation of a file descriptor into an
|
2002-10-20 15:04:52 +04:00
|
|
|
internal representation.
|
|
|
|
The file descriptor is merely an index into this table.
|
2019-04-08 16:04:54 +03:00
|
|
|
The table maintains the following information:
|
2002-10-20 09:16:31 +04:00
|
|
|
.Pp
|
|
|
|
.Bl -bullet -compact
|
|
|
|
.It
|
|
|
|
the number of descriptors allocated in the file descriptor table;
|
|
|
|
.It
|
|
|
|
approximate next free descriptor;
|
|
|
|
.It
|
|
|
|
a reference count on the file descriptor table; and
|
|
|
|
.It
|
|
|
|
an array of open file entries.
|
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
On creation of the file descriptor table, a fixed number of file
|
2002-10-20 15:04:52 +04:00
|
|
|
entries are created.
|
|
|
|
It is the responsibility of the file descriptor operations to expand the
|
|
|
|
available number of entries if more are required.
|
2019-04-08 16:04:54 +03:00
|
|
|
Each file entry in the descriptor table contains the information needed
|
2002-10-20 15:04:52 +04:00
|
|
|
to access the underlying object and to maintain common information.
|
|
|
|
See
|
2002-10-20 09:16:31 +04:00
|
|
|
.Xr file 9
|
|
|
|
for details of operations on the file entries.
|
|
|
|
.Pp
|
|
|
|
New file descriptors are generally allocated by
|
2019-04-08 16:04:54 +03:00
|
|
|
.Fn fd_alloc
|
2002-10-20 09:16:31 +04:00
|
|
|
and freed by
|
2019-04-08 16:04:54 +03:00
|
|
|
.Fn fd_free .
|
2002-10-20 09:16:31 +04:00
|
|
|
File entries are extracted from the file descriptor table by
|
|
|
|
.Fn fd_getfile .
|
2019-04-08 16:04:54 +03:00
|
|
|
Most of the remaining functions in the interface are purpose-specific
|
2002-10-20 09:16:31 +04:00
|
|
|
and perform lower-level file descriptor operations.
|
|
|
|
.Sh FUNCTIONS
|
|
|
|
The following functions are high-level interface routines to access
|
|
|
|
the file descriptor table for a process and its file entries.
|
|
|
|
.Bl -tag -width compact
|
2019-04-08 16:04:54 +03:00
|
|
|
.It Fn fd_alloc "p" "want" "*result"
|
|
|
|
Create a new open file entry in the file descriptor table and
|
|
|
|
allocate a file descriptor for the process
|
2006-01-29 19:12:57 +03:00
|
|
|
.Fa p .
|
2006-01-29 21:08:54 +03:00
|
|
|
The credential on the file entry are inherited from process
|
|
|
|
.Fa p .
|
2019-04-08 16:30:46 +03:00
|
|
|
Calling the
|
2019-04-08 16:04:54 +03:00
|
|
|
.Fn fd_alloc
|
2019-04-08 16:30:46 +03:00
|
|
|
function expands the file descriptor table when necessary.
|
2002-10-20 09:16:31 +04:00
|
|
|
.Pp
|
2019-04-08 16:04:54 +03:00
|
|
|
The index of the file entry is returned in
|
|
|
|
.Fa *result .
|
2002-10-20 09:16:31 +04:00
|
|
|
The
|
2019-04-08 16:04:54 +03:00
|
|
|
.Fn fd_alloc
|
|
|
|
function returns zero on success, or an appropriate error value
|
|
|
|
otherwise.
|
|
|
|
.It Fn fd_getfile "fd"
|
|
|
|
Get the file entry for file descriptor .
|
2002-10-20 09:16:31 +04:00
|
|
|
.Fa fd
|
2019-04-08 16:04:54 +03:00
|
|
|
The file entry is returned if it is valid and usable, otherwise
|
2002-10-20 15:04:52 +04:00
|
|
|
.Dv NULL
|
2002-10-20 09:16:31 +04:00
|
|
|
is returned.
|
2019-04-08 16:04:54 +03:00
|
|
|
.It Fn fd_dup "fp" "minfd" "*newp" "exclose"
|
|
|
|
Duplicate file descriptor
|
|
|
|
.Fa fp
|
|
|
|
for the current process.
|
|
|
|
The fd picked will be at least
|
|
|
|
.Fa minfd .
|
|
|
|
The resulting descriptor is given in
|
|
|
|
.Fa newp .
|
|
|
|
.It Fn fd_dup2 "fp" "newfd" "flags"
|
2002-10-20 09:16:31 +04:00
|
|
|
Duplicate file descriptor
|
2019-04-08 16:04:54 +03:00
|
|
|
.Fa fp
|
|
|
|
in fd number
|
|
|
|
.Fa newfd .
|
|
|
|
If newfd is already in use by an open file, close it (See
|
|
|
|
.Xr dup2 2 ) .
|
|
|
|
.It Fn fd_dupopen "old" "*newp" "error"
|
|
|
|
Duplicate file descriptor specified in
|
|
|
|
.Fa old
|
|
|
|
for the current lwp.
|
2002-10-20 09:16:31 +04:00
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
The following functions operate on the file descriptor table for a
|
|
|
|
process.
|
|
|
|
.Bl -tag -width compact
|
2019-04-08 16:04:54 +03:00
|
|
|
.It Fn fd_alloc "p" "want" "*result"
|
2002-10-20 09:16:31 +04:00
|
|
|
Allocate a file descriptor
|
|
|
|
.Fa want
|
2006-02-25 01:04:15 +03:00
|
|
|
for process
|
2006-01-29 21:08:54 +03:00
|
|
|
.Fa p .
|
2002-10-20 09:16:31 +04:00
|
|
|
The resultant file descriptor is returned in
|
|
|
|
.Fa *result .
|
|
|
|
The
|
2019-04-08 16:04:54 +03:00
|
|
|
.Fn fd_alloc
|
2002-10-20 09:16:31 +04:00
|
|
|
function returns zero on success, otherwise an appropriate error is
|
|
|
|
returned.
|
2019-04-08 16:04:54 +03:00
|
|
|
.It Fn fd_clone "fp" "fd" "flag" "fops" "data"
|
2006-02-24 15:47:18 +03:00
|
|
|
This function is meant to be used by devices which allocate a file
|
|
|
|
entry upon open.
|
2019-04-08 16:04:54 +03:00
|
|
|
.Fn fd_clone
|
2006-02-24 15:47:18 +03:00
|
|
|
fills
|
|
|
|
.Fa fp
|
|
|
|
with the given parameters.
|
2019-04-08 16:04:54 +03:00
|
|
|
It always returns the in-kernel errno
|
|
|
|
.Er EMOVEFD .
|
2006-02-24 15:47:18 +03:00
|
|
|
This special return value is interpreted by the caller of the device
|
|
|
|
open routine.
|
2019-04-08 16:04:54 +03:00
|
|
|
.It Fn fd_closeexec "void"
|
|
|
|
Close any files for the current process
|
2002-10-20 15:04:52 +04:00
|
|
|
that are marked
|
|
|
|
.Dq close on exec .
|
|
|
|
This operation is performed by invoking
|
2019-04-08 16:04:54 +03:00
|
|
|
.Fn fd_close
|
2002-10-20 09:16:31 +04:00
|
|
|
on the appropriate file descriptor.
|
2019-04-08 16:04:54 +03:00
|
|
|
.It Fn fd_copy "void"
|
|
|
|
Copy the file descriptor table from the current process
|
2002-10-20 15:04:52 +04:00
|
|
|
and return a pointer to the copy.
|
|
|
|
The returned file descriptor is guaranteed to have a reference count of one.
|
|
|
|
All file descriptor state is maintained.
|
|
|
|
The reference counts on each file entry referenced by the file
|
|
|
|
descriptor table is incremented accordingly.
|
2019-04-08 16:04:54 +03:00
|
|
|
.It Fn fd_tryexpand "p"
|
2006-01-29 21:08:54 +03:00
|
|
|
Expand the file descriptor table for process
|
|
|
|
.Fa p
|
2002-10-20 09:16:31 +04:00
|
|
|
by allocating memory for additional file descriptors.
|
2019-04-08 16:04:54 +03:00
|
|
|
.It Fn fd_free "void"
|
|
|
|
Decrement the reference count on the file descriptor table for the current lwp
|
2002-10-20 09:16:31 +04:00
|
|
|
and release the file descriptor table if the reference count drops to
|
|
|
|
zero.
|
2019-04-08 16:04:54 +03:00
|
|
|
.It Fn fd_share "p"
|
|
|
|
Make process
|
|
|
|
.Fa p
|
|
|
|
share the current process's filedesc structure.
|
|
|
|
.It Fn fd_checkstd "l"
|
|
|
|
Check the standard file descriptors 0, 1, and 2 and ensure they are
|
|
|
|
referencing valid file descriptors.
|
|
|
|
If they are not, create references to
|
|
|
|
.Pa /dev/null .
|
|
|
|
This is done to setuid/setgid executables, as file descriptors 0, 1, 2
|
|
|
|
are implicitly used by the Standard C Library.
|
|
|
|
.It Fn fd_init "fdp"
|
2002-10-20 09:16:31 +04:00
|
|
|
Create a file descriptor table using the same current and root
|
2019-04-08 16:04:54 +03:00
|
|
|
directories of the current process.
|
2002-10-20 09:16:31 +04:00
|
|
|
The returned file descriptor table is guaranteed to have a reference
|
|
|
|
count of one.
|
|
|
|
.El
|
|
|
|
.Sh RETURN VALUES
|
2002-10-20 15:04:52 +04:00
|
|
|
Successful operations return zero.
|
2019-04-08 16:04:54 +03:00
|
|
|
A failed operation will return a non-zero value.
|
2002-10-20 15:04:52 +04:00
|
|
|
Possible values include:
|
2002-10-20 09:16:31 +04:00
|
|
|
.Bl -tag -width Er
|
|
|
|
.It Bq Er EBADF
|
|
|
|
Bad file descriptor specified.
|
|
|
|
.It Bq Er EMFILE
|
|
|
|
Cannot exceed file descriptor limit.
|
|
|
|
.It Bq Er ENOSPC
|
|
|
|
No space left in file descriptor table.
|
|
|
|
.El
|
|
|
|
.Sh CODE REFERENCES
|
|
|
|
The framework for file descriptor handling is implemented within the
|
|
|
|
file
|
|
|
|
.Pa sys/kern/kern_descrip.c .
|
|
|
|
.Sh SEE ALSO
|
|
|
|
.Xr file 9
|