xref: /freebsd-12.1/lib/libc/sys/close.2 (revision aeb71118)
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.\"     @(#)close.2	8.2 (Berkeley) 4/19/94
29.\" $FreeBSD$
30.\"
31.Dd December 1, 2017
32.Dt CLOSE 2
33.Os
34.Sh NAME
35.Nm close
36.Nd delete a descriptor
37.Sh LIBRARY
38.Lb libc
39.Sh SYNOPSIS
40.In unistd.h
41.Ft int
42.Fn close "int fd"
43.Sh DESCRIPTION
44The
45.Fn close
46system call deletes a descriptor from the per-process object
47reference table.
48If this is the last reference to the underlying object, the
49object will be deactivated.
50For example, on the last close of a file
51the current
52.Em seek
53pointer associated with the file is lost;
54on the last close of a
55.Xr socket 2
56associated naming information and queued data are discarded;
57on the last close of a file holding an advisory lock
58the lock is released (see further
59.Xr flock 2 ) .
60However, the semantics of System V and
61.St -p1003.1-88
62dictate that all
63.Xr fcntl 2
64advisory record locks associated with a file for a given process
65are removed when
66.Em any
67file descriptor for that file is closed by that process.
68.Pp
69When a process exits,
70all associated file descriptors are freed, but since there is
71a limit on active descriptors per processes, the
72.Fn close
73system call
74is useful when a large quantity of file descriptors are being handled.
75.Pp
76When a process forks (see
77.Xr fork 2 ) ,
78all descriptors for the new child process reference the same
79objects as they did in the parent before the fork.
80If a new process is then to be run using
81.Xr execve 2 ,
82the process would normally inherit these descriptors.
83Most
84of the descriptors can be rearranged with
85.Xr dup2 2
86or deleted with
87.Fn close
88before the
89.Xr execve 2
90is attempted, but if some of these descriptors will still
91be needed if the execve fails, it is necessary to arrange for them
92to be closed if the execve succeeds.
93For this reason, the call
94.Dq Li fcntl(d, F_SETFD, FD_CLOEXEC)
95is provided,
96which arranges that a descriptor will be closed after a successful
97execve; the call
98.Dq Li fcntl(d, F_SETFD, 0)
99restores the default,
100which is to not close the descriptor.
101.Sh RETURN VALUES
102.Rv -std close
103.Sh ERRORS
104The
105.Fn close
106system call will fail if:
107.Bl -tag -width Er
108.It Bq Er EBADF
109The
110.Fa fd
111argument
112is not an active descriptor.
113.It Bq Er EINTR
114An interrupt was received.
115.It Bq Er ENOSPC
116The underlying object did not fit, cached data was lost.
117.It Bq Er ECONNRESET
118The underlying object was a stream socket that was shut down by the peer
119before all pending data was delivered.
120.El
121.Pp
122In case of any error except
123.Er EBADF ,
124the supplied file descriptor is deallocated and therefore is no longer valid.
125.Sh SEE ALSO
126.Xr accept 2 ,
127.Xr closefrom 2 ,
128.Xr execve 2 ,
129.Xr fcntl 2 ,
130.Xr flock 2 ,
131.Xr open 2 ,
132.Xr pipe 2 ,
133.Xr socket 2 ,
134.Xr socketpair 2
135.Sh STANDARDS
136The
137.Fn close
138system call is expected to conform to
139.St -p1003.1-90 .
140.Sh HISTORY
141The
142.Fn close
143function appeared in
144.At v1 .
145