Debian Codegraph @

upstream/debian-unstable/man2/close.2

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