Debian Codegraph @

upstream/fedora-44/man2/close.2

raw
1 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
2 .\" and Copyright (C) 1993 Michael Haardt, Ian Jackson.
3 .\" and Copyright (C) 2016 Michael Kerrisk <[email protected]>
4 .\"
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\"
7 .\" Modified Wed Jul 21 22:40:25 1993 by Rik Faith <[email protected]>
8 .\" Modified Sat Feb 18 15:27:48 1995 by Michael Haardt
9 .\" Modified Sun Apr 14 11:40:50 1996 by Andries Brouwer <[email protected]>:
10 .\" corrected description of effect on locks (thanks to
11 .\" Tigran Aivazian <[email protected]>).
12 .\" Modified Fri Jan 31 16:21:46 1997 by Eric S. Raymond <[email protected]>
13 .\" Modified 2000-07-22 by Nicolás Lichtmaier <[email protected]>
14 .\" added note about close(2) not guaranteeing that data is safe on close.
15 .\"
16 .TH close 2 2024-07-23 "Linux man-pages 6.13"
17 .SH NAME
18 close \- close a file descriptor
19 .SH LIBRARY
20 Standard C library
21 .RI ( libc ,\~ \-lc )
22 .SH SYNOPSIS
23 .nf
24 .B #include <unistd.h>
25 .P
26 .BI "int close(int " fd );
27 .fi
28 .SH DESCRIPTION
29 .BR close ()
30 closes a file descriptor, so that it no longer refers to any file and
31 may be reused.
32 Any record locks (see
33 .BR fcntl (2))
34 held on the file it was associated with,
35 and owned by the process,
36 are removed regardless of the file descriptor that was used to obtain the lock.
37 This has some unfortunate consequences
38 and one should be extra careful when using advisory record locking.
39 See
40 .BR fcntl (2)
41 for discussion of the risks and consequences
42 as well as for the (probably preferred) open file description locks.
43 .P
44 If
45 .I fd
46 is the last file descriptor referring to the underlying
47 open file description (see
48 .BR open (2)),
49 the resources associated with the open file description are freed;
50 if the file descriptor was the last reference to a file which has been
51 removed using
52 .BR unlink (2),
53 the file is deleted.
54 .SH RETURN VALUE
55 .BR close ()
56 returns zero on success.
57 On error, \-1 is returned, and
58 .I errno
59 is set to indicate the error.
60 .SH ERRORS
61 .TP
62 .B EBADF
63 .I fd
64 isn't a valid open file descriptor.
65 .TP
66 .B EINTR
67 .\" Though, it's in doubt whether this error can ever occur; see
68 .\" https://lwn.net/Articles/576478/ "Returning EINTR from close()"
69 The
70 .BR close ()
71 call was interrupted by a signal; see
72 .BR signal (7).
73 .TP
74 .B EIO
75 An I/O error occurred.
76 .TP
77 .B ENOSPC
78 .TQ
79 .B EDQUOT
80 On NFS, these errors are not normally reported against the first write
81 which exceeds the available storage space, but instead against a
82 subsequent
83 .BR write (2),
84 .BR fsync (2),
85 or
86 .BR close ().
87 .P
88 See NOTES for a discussion of why
89 .BR close ()
90 should not be retried after an error.
91 .SH STANDARDS
92 POSIX.1-2008.
93 .SH HISTORY
94 POSIX.1-2001, SVr4, 4.3BSD.
95 .\" SVr4 documents an additional ENOLINK error condition.
96 .SH NOTES
97 A successful close does not guarantee that the data has been successfully
98 saved to disk, as the kernel uses the buffer cache to defer writes.
99 Typically, filesystems do not flush buffers when a file is closed.
100 If you need to be sure that
101 the data is physically stored on the underlying disk, use
102 .BR fsync (2).
103 (It will depend on the disk hardware at this point.)
104 .P
105 The close-on-exec file descriptor flag can be used to ensure
106 that a file descriptor is automatically closed upon a successful
107 .BR execve (2);
108 see
109 .BR fcntl (2)
110 for details.
111 .\"
112 .SS Multithreaded processes and close()
113 It is probably unwise to close file descriptors while
114 they may be in use by system calls in
115 other threads in the same process.
116 Since a file descriptor may be reused,
117 there are some obscure race conditions
118 that may cause unintended side effects.
119 .PP
120 When dealing with sockets, you have to be sure that there is no
121 .BR recv (2)
122 still blocking on it on another thread, otherwise it might block forever,
123 since no more messages will be send via the socket. Be sure to use
124 .BR shutdown (2)
125 to shut down all parts the connection before closing the socket.
126 .\" Date: Tue, 4 Sep 2007 13:57:35 +0200
127 .\" From: Fredrik Noring <[email protected]>
128 .\" One such race involves signals and ERESTARTSYS. If a file descriptor
129 .\" in use by a system call is closed and then reused by e.g. an
130 .\" independent open() in some unrelated thread, before the original system
131 .\" call has restarted after ERESTARTSYS, the original system call will
132 .\" later restart with the reused file descriptor. This is most likely a
133 .\" serious programming error.
134 .P
135 Furthermore, consider the following scenario where two threads are
136 performing operations on the same file descriptor:
137 .IP (1) 5
138 One thread is blocked in an I/O system call on the file descriptor.
139 For example, it is trying to
140 .BR write (2)
141 to a pipe that is already full, or trying to
142 .BR read (2)
143 from a stream socket which currently has no available data.
144 .IP (2)
145 Another thread closes the file descriptor.
146 .P
147 The behavior in this situation varies across systems.
148 On some systems, when the file descriptor is closed,
149 the blocking system call returns immediately with an error.
150 .P
151 On Linux (and possibly some other systems), the behavior is different:
152 the blocking I/O system call holds a reference to the underlying
153 open file description, and this reference keeps the description open
154 until the I/O system call completes.
155 .\" 'struct file' in kernel-speak
156 (See
157 .BR open (2)
158 for a discussion of open file descriptions.)
159 Thus, the blocking system call in the first thread may successfully
160 complete after the
161 .BR close ()
162 in the second thread.
163 .\"
164 .SS Dealing with error returns from close()
165 A careful programmer will check the return value of
166 .BR close (),
167 since it is quite possible that errors on a previous
168 .BR write (2)
169 operation are reported only on the final
170 .BR close ()
171 that releases the open file description.
172 Failing to check the return value when closing a file may lead to
173 .I silent
174 loss of data.
175 This can especially be observed with NFS and with disk quota.
176 .P
177 Note, however, that a failure return should be used only for
178 diagnostic purposes (i.e., a warning to the application that there
179 may still be I/O pending or there may have been failed I/O)
180 or remedial purposes
181 (e.g., writing the file once more or creating a backup).
182 .P
183 Retrying the
184 .BR close ()
185 after a failure return is the wrong thing to do,
186 .\" The file descriptor is released early in close();
187 .\" close() ==> __close_fd():
188 .\" __put_unused_fd() ==> __clear_open_fd()
189 .\" return filp_close(file, files);
190 .\"
191 .\" The errors are returned by filp_close() after the FD has been
192 .\" cleared for re-use.
193 since this may cause a reused file descriptor
194 from another thread to be closed.
195 This can occur because the Linux kernel
196 .I always
197 releases the file descriptor early in the close
198 operation, freeing it for reuse;
199 the steps that may return an error,
200 .\" filp_close()
201 such as flushing data to the filesystem or device,
202 occur only later in the close operation.
203 .P
204 Many other implementations similarly always close the file descriptor
205 .\" FreeBSD documents this explicitly. From the look of the source code
206 .\" SVR4, ancient SunOS, later Solaris, and AIX all do this.
207 (except in the case of
208 .BR EBADF ,
209 meaning that the file descriptor was invalid)
210 even if they subsequently report an error on return from
211 .BR close ().
212 POSIX.1 is currently silent on this point,
213 but there are plans to mandate this behavior in the next major release
214 .\" Issue 8
215 of the standard.
216 .P
217 A careful programmer who wants to know about I/O errors may precede
218 .BR close ()
219 with a call to
220 .BR fsync (2).
221 .P
222 The
223 .B EINTR
224 error is a somewhat special case.
225 Regarding the
226 .B EINTR
227 error, POSIX.1-2008 says:
228 .P
229 .RS
230 If
231 .BR close ()
232 is interrupted by a signal that is to be caught, it shall return \-1 with
233 .I errno
234 set to
235 .B EINTR
236 and the state of
237 .I fildes
238 is unspecified.
239 .RE
240 .P
241 This permits the behavior that occurs on Linux and
242 many other implementations, where,
243 as with other errors that may be reported by
244 .BR close (),
245 the file descriptor is guaranteed to be closed.
246 However, it also permits another possibility:
247 that the implementation returns an
248 .B EINTR
249 error and keeps the file descriptor open.
250 (According to its documentation, HP-UX's
251 .BR close ()
252 does this.)
253 The caller must then once more use
254 .BR close ()
255 to close the file descriptor, to avoid file descriptor leaks.
256 This divergence in implementation behaviors provides
257 a difficult hurdle for portable applications, since on many implementations,
258 .BR close ()
259 must not be called again after an
260 .B EINTR
261 error, and on at least one,
262 .BR close ()
263 must be called again.
264 There are plans to address this conundrum for
265 the next major release of the POSIX.1 standard.
266 .\" FIXME . for later review when Issue 8 is one day released...
267 .\" POSIX proposes further changes for EINTR
268 .\" http://austingroupbugs.net/tag_view_page.php?tag_id=8
269 .\" http://austingroupbugs.net/view.php?id=529
270 .\"
271 .\" FIXME .
272 .\" Review the following glibc bug later
273 .\" https://sourceware.org/bugzilla/show_bug.cgi?id=14627
274 .SH SEE ALSO
275 .BR close_range (2),
276 .BR fcntl (2),
277 .BR fsync (2),
278 .BR open (2),
279 .BR shutdown (2),
280 .BR unlink (2),
281 .BR fclose (3)