xref: /freebsd/lib/libsys/close.2 (revision a91a2465)
1.\" Copyright (c) 1980, 1991, 1993, 1994
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.Dd December 1, 2017
29.Dt CLOSE 2
30.Os
31.Sh NAME
32.Nm close
33.Nd delete a descriptor
34.Sh LIBRARY
35.Lb libc
36.Sh SYNOPSIS
37.In unistd.h
38.Ft int
39.Fn close "int fd"
40.Sh DESCRIPTION
41The
42.Fn close
43system call deletes a descriptor from the per-process object
44reference table.
45If this is the last reference to the underlying object, the
46object will be deactivated.
47For example, on the last close of a file
48the current
49.Em seek
50pointer associated with the file is lost;
51on the last close of a
52.Xr socket 2
53associated naming information and queued data are discarded;
54on the last close of a file holding an advisory lock
55the lock is released (see further
56.Xr flock 2 ) .
57However, the semantics of System V and
58.St -p1003.1-88
59dictate that all
60.Xr fcntl 2
61advisory record locks associated with a file for a given process
62are removed when
63.Em any
64file descriptor for that file is closed by that process.
65.Pp
66When a process exits,
67all associated file descriptors are freed, but since there is
68a limit on active descriptors per processes, the
69.Fn close
70system call
71is useful when a large quantity of file descriptors are being handled.
72.Pp
73When a process forks (see
74.Xr fork 2 ) ,
75all descriptors for the new child process reference the same
76objects as they did in the parent before the fork.
77If a new process is then to be run using
78.Xr execve 2 ,
79the process would normally inherit these descriptors.
80Most
81of the descriptors can be rearranged with
82.Xr dup2 2
83or deleted with
84.Fn close
85before the
86.Xr execve 2
87is attempted, but if some of these descriptors will still
88be needed if the execve fails, it is necessary to arrange for them
89to be closed if the execve succeeds.
90For this reason, the call
91.Dq Li fcntl(d, F_SETFD, FD_CLOEXEC)
92is provided,
93which arranges that a descriptor will be closed after a successful
94execve; the call
95.Dq Li fcntl(d, F_SETFD, 0)
96restores the default,
97which is to not close the descriptor.
98.Sh RETURN VALUES
99.Rv -std close
100.Sh ERRORS
101The
102.Fn close
103system call will fail if:
104.Bl -tag -width Er
105.It Bq Er EBADF
106The
107.Fa fd
108argument
109is not an active descriptor.
110.It Bq Er EINTR
111An interrupt was received.
112.It Bq Er ENOSPC
113The underlying object did not fit, cached data was lost.
114.It Bq Er ECONNRESET
115The underlying object was a stream socket that was shut down by the peer
116before all pending data was delivered.
117.El
118.Pp
119In case of any error except
120.Er EBADF ,
121the supplied file descriptor is deallocated and therefore is no longer valid.
122.Sh SEE ALSO
123.Xr accept 2 ,
124.Xr closefrom 2 ,
125.Xr execve 2 ,
126.Xr fcntl 2 ,
127.Xr flock 2 ,
128.Xr open 2 ,
129.Xr pipe 2 ,
130.Xr socket 2 ,
131.Xr socketpair 2
132.Sh STANDARDS
133The
134.Fn close
135system call is expected to conform to
136.St -p1003.1-90 .
137.Sh HISTORY
138The
139.Fn close
140function appeared in
141.At v1 .
142