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 <mtk.manpages@gmail.com>
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
7 .\" Modified Wed Jul 21 22:40:25 1993 by Rik Faith <faith@cs.unc.edu>
8 .\" Modified Sat Feb 18 15:27:48 1995 by Michael Haardt
9 .\" Modified Sun Apr 14 11:40:50 1996 by Andries Brouwer <aeb@cwi.nl>:
10 .\" corrected description of effect on locks (thanks to
11 .\" Tigran Aivazian <tigran@sco.com>).
12 .\" Modified Fri Jan 31 16:21:46 1997 by Eric S. Raymond <esr@thyrsus.com>
13 .\" Modified 2000-07-22 by Nicolás Lichtmaier <nick@debian.org>
14 .\" added note about close(2) not guaranteeing that data is safe on close.
16 .TH close 2 (date) "Linux man-pages (unreleased)"
18 close \- close a file descriptor
21 .RI ( libc ", " \-lc )
24 .B #include <unistd.h>
26 .BI "int close(int " fd );
30 closes a file descriptor, so that it no longer refers to any file and
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.
41 for discussion of the risks and consequences
42 as well as for the (probably preferred) open file description locks.
46 is the last file descriptor referring to the underlying
47 open file description (see
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
56 returns zero on success.
57 On error, \-1 is returned, and
59 is set to indicate the error.
64 isn't a valid open file descriptor.
67 .\" Though, it's in doubt whether this error can ever occur; see
68 .\" https://lwn.net/Articles/576478/ "Returning EINTR from close()"
71 call was interrupted by a signal; see
75 An I/O error occurred.
80 On NFS, these errors are not normally reported against the first write
81 which exceeds the available storage space, but instead against a
88 See NOTES for a discussion of why
90 should not be retried after an error.
94 POSIX.1-2001, SVr4, 4.3BSD.
95 .\" SVr4 documents an additional ENOLINK error condition.
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
103 (It will depend on the disk hardware at this point.)
105 The close-on-exec file descriptor flag can be used to ensure
106 that a file descriptor is automatically closed upon a successful
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 .\" Date: Tue, 4 Sep 2007 13:57:35 +0200
120 .\" From: Fredrik Noring <noring@nocrew.org>
121 .\" One such race involves signals and ERESTARTSYS. If a file descriptor
122 .\" in use by a system call is closed and then reused by e.g. an
123 .\" independent open() in some unrelated thread, before the original system
124 .\" call has restarted after ERESTARTSYS, the original system call will
125 .\" later restart with the reused file descriptor. This is most likely a
126 .\" serious programming error.
128 Furthermore, consider the following scenario where two threads are
129 performing operations on the same file descriptor:
131 One thread is blocked in an I/O system call on the file descriptor.
132 For example, it is trying to
134 to a pipe that is already full, or trying to
136 from a stream socket which currently has no available data.
138 Another thread closes the file descriptor.
140 The behavior in this situation varies across systems.
141 On some systems, when the file descriptor is closed,
142 the blocking system call returns immediately with an error.
144 On Linux (and possibly some other systems), the behavior is different:
145 the blocking I/O system call holds a reference to the underlying
146 open file description, and this reference keeps the description open
147 until the I/O system call completes.
148 .\" 'struct file' in kernel-speak
151 for a discussion of open file descriptions.)
152 Thus, the blocking system call in the first thread may successfully
155 in the second thread.
157 .SS Dealing with error returns from close()
158 A careful programmer will check the return value of
160 since it is quite possible that errors on a previous
162 operation are reported only on the final
164 that releases the open file description.
165 Failing to check the return value when closing a file may lead to
168 This can especially be observed with NFS and with disk quota.
170 Note, however, that a failure return should be used only for
171 diagnostic purposes (i.e., a warning to the application that there
172 may still be I/O pending or there may have been failed I/O)
174 (e.g., writing the file once more or creating a backup).
178 after a failure return is the wrong thing to do,
179 .\" The file descriptor is released early in close();
180 .\" close() ==> __close_fd():
181 .\" __put_unused_fd() ==> __clear_open_fd()
182 .\" return filp_close(file, files);
184 .\" The errors are returned by filp_close() after the FD has been
185 .\" cleared for re-use.
186 since this may cause a reused file descriptor
187 from another thread to be closed.
188 This can occur because the Linux kernel
190 releases the file descriptor early in the close
191 operation, freeing it for reuse;
192 the steps that may return an error,
194 such as flushing data to the filesystem or device,
195 occur only later in the close operation.
197 Many other implementations similarly always close the file descriptor
198 .\" FreeBSD documents this explicitly. From the look of the source code
199 .\" SVR4, ancient SunOS, later Solaris, and AIX all do this.
200 (except in the case of
202 meaning that the file descriptor was invalid)
203 even if they subsequently report an error on return from
205 POSIX.1 is currently silent on this point,
206 but there are plans to mandate this behavior in the next major release
210 A careful programmer who wants to know about I/O errors may precede
217 error is a somewhat special case.
220 error, POSIX.1-2008 says:
225 is interrupted by a signal that is to be caught, it shall return \-1 with
234 This permits the behavior that occurs on Linux and
235 many other implementations, where,
236 as with other errors that may be reported by
238 the file descriptor is guaranteed to be closed.
239 However, it also permits another possibility:
240 that the implementation returns an
242 error and keeps the file descriptor open.
243 (According to its documentation, HP-UX's
246 The caller must then once more use
248 to close the file descriptor, to avoid file descriptor leaks.
249 This divergence in implementation behaviors provides
250 a difficult hurdle for portable applications, since on many implementations,
252 must not be called again after an
254 error, and on at least one,
256 must be called again.
257 There are plans to address this conundrum for
258 the next major release of the POSIX.1 standard.
259 .\" FIXME . for later review when Issue 8 is one day released...
260 .\" POSIX proposes further changes for EINTR
261 .\" http://austingroupbugs.net/tag_view_page.php?tag_id=8
262 .\" http://austingroupbugs.net/view.php?id=529
265 .\" Review the following glibc bug later
266 .\" https://sourceware.org/bugzilla/show_bug.cgi?id=14627