1CLOSE(3P) POSIX Programmer's Manual CLOSE(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
11
13 close — close a file descriptor
14
16 #include <unistd.h>
17
18 int close(int fildes);
19
21 The close() function shall deallocate the file descriptor indicated by
22 fildes. To deallocate means to make the file descriptor available for
23 return by subsequent calls to open() or other functions that allocate
24 file descriptors. All outstanding record locks owned by the process on
25 the file associated with the file descriptor shall be removed (that is,
26 unlocked).
27
28 If close() is interrupted by a signal that is to be caught, it shall
29 return −1 with errno set to [EINTR] and the state of fildes is unspeci‐
30 fied. If an I/O error occurred while reading from or writing to the
31 file system during close(), it may return −1 with errno set to [EIO];
32 if this error is returned, the state of fildes is unspecified.
33
34 When all file descriptors associated with a pipe or FIFO special file
35 are closed, any data remaining in the pipe or FIFO shall be discarded.
36
37 When all file descriptors associated with an open file description have
38 been closed, the open file description shall be freed.
39
40 If the link count of the file is 0, when all file descriptors associ‐
41 ated with the file are closed, the space occupied by the file shall be
42 freed and the file shall no longer be accessible.
43
44 If a STREAMS-based fildes is closed and the calling process was previ‐
45 ously registered to receive a SIGPOLL signal for events associated with
46 that STREAM, the calling process shall be unregistered for events asso‐
47 ciated with the STREAM. The last close() for a STREAM shall cause the
48 STREAM associated with fildes to be dismantled. If O_NONBLOCK is not
49 set and there have been no signals posted for the STREAM, and if there
50 is data on the module's write queue, close() shall wait for an unspeci‐
51 fied time (for each module and driver) for any output to drain before
52 dismantling the STREAM. The time delay can be changed via an I_SET‐
53 CLTIME ioctl() request. If the O_NONBLOCK flag is set, or if there are
54 any pending signals, close() shall not wait for output to drain, and
55 shall dismantle the STREAM immediately.
56
57 If the implementation supports STREAMS-based pipes, and fildes is asso‐
58 ciated with one end of a pipe, the last close() shall cause a hangup to
59 occur on the other end of the pipe. In addition, if the other end of
60 the pipe has been named by fattach(), then the last close() shall force
61 the named end to be detached by fdetach(). If the named end has no
62 open file descriptors associated with it and gets detached, the STREAM
63 associated with that end shall also be dismantled.
64
65 If fildes refers to the master side of a pseudo-terminal, and this is
66 the last close, a SIGHUP signal shall be sent to the controlling
67 process, if any, for which the slave side of the pseudo-terminal is the
68 controlling terminal. It is unspecified whether closing the master side
69 of the pseudo-terminal flushes all queued input and output.
70
71 If fildes refers to the slave side of a STREAMS-based pseudo-terminal,
72 a zero-length message may be sent to the master.
73
74 When there is an outstanding cancelable asynchronous I/O operation
75 against fildes when close() is called, that I/O operation may be can‐
76 celed. An I/O operation that is not canceled completes as if the
77 close() operation had not yet occurred. All operations that are not
78 canceled shall complete as if the close() blocked until the operations
79 completed. The close() operation itself need not block awaiting such
80 I/O completion. Whether any I/O operation is canceled, and which I/O
81 operation may be canceled upon close(), is implementation-defined.
82
83 If a memory mapped file or a shared memory object remains referenced at
84 the last close (that is, a process has it mapped), then the entire con‐
85 tents of the memory object shall persist until the memory object
86 becomes unreferenced. If this is the last close of a memory mapped
87 file or a shared memory object and the close results in the memory
88 object becoming unreferenced, and the memory object has been unlinked,
89 then the memory object shall be removed.
90
91 If fildes refers to a socket, close() shall cause the socket to be
92 destroyed. If the socket is in connection-mode, and the SO_LINGER
93 option is set for the socket with non-zero linger time, and the socket
94 has untransmitted data, then close() shall block for up to the current
95 linger interval until all data is transmitted.
96
98 Upon successful completion, 0 shall be returned; otherwise, −1 shall be
99 returned and errno set to indicate the error.
100
102 The close() function shall fail if:
103
104 EBADF The fildes argument is not a open file descriptor.
105
106 EINTR The close() function was interrupted by a signal.
107
108 The close() function may fail if:
109
110 EIO An I/O error occurred while reading from or writing to the file
111 system.
112
113 The following sections are informative.
114
116 Reassigning a File Descriptor
117 The following example closes the file descriptor associated with stan‐
118 dard output for the current process, re-assigns standard output to a
119 new file descriptor, and closes the original file descriptor to clean
120 up. This example assumes that the file descriptor 0 (which is the
121 descriptor for standard input) is not closed.
122
123 #include <unistd.h>
124 ...
125 int pfd;
126 ...
127 close(1);
128 dup(pfd);
129 close(pfd);
130 ...
131
132 Incidentally, this is exactly what could be achieved using:
133
134 dup2(pfd, 1);
135 close(pfd);
136
137 Closing a File Descriptor
138 In the following example, close() is used to close a file descriptor
139 after an unsuccessful attempt is made to associate that file descriptor
140 with a stream.
141
142 #include <stdio.h>
143 #include <unistd.h>
144 #include <stdlib.h>
145
146 #define LOCKFILE "/etc/ptmp"
147 ...
148 int pfd;
149 FILE *fpfd;
150 ...
151 if ((fpfd = fdopen (pfd, "w")) == NULL) {
152 close(pfd);
153 unlink(LOCKFILE);
154 exit(1);
155 }
156 ...
157
159 An application that had used the stdio routine fopen() to open a file
160 should use the corresponding fclose() routine rather than close().
161 Once a file is closed, the file descriptor no longer exists, since the
162 integer corresponding to it no longer refers to a file.
163
164 Implementations may use file descriptors that must be inherited into
165 child processes for the child process to remain conforming, such as for
166 message catalog or tracing purposes. Therefore, an application that
167 calls close() on an arbitrary integer risks non-conforming behavior,
168 and close() can only portably be used on file descriptor values that
169 the application has obtained through explicit actions, as well as the
170 three file descriptors corresponding to the standard file streams. In
171 multi-threaded parent applications, the practice of calling close() in
172 a loop after fork() and before an exec call in order to avoid a race
173 condition of leaking an unintended file descriptor into a child
174 process, is therefore unsafe, and the race should instead be combatted
175 by opening all file descriptors with the FD_CLOEXEC bit set unless the
176 file descriptor is intended to be inherited across exec.
177
179 The use of interruptible device close routines should be discouraged to
180 avoid problems with the implicit closes of file descriptors by exec and
181 exit(). This volume of POSIX.1‐2008 only intends to permit such behav‐
182 ior by specifying the [EINTR] error condition.
183
184 Note that the requirement for close() on a socket to block for up to
185 the current linger interval is not conditional on the O_NONBLOCK set‐
186 ting.
187
188 The standard developers rejected a proposal to add closefrom() to the
189 standard. Because the standard permits implementations to use inherited
190 file descriptors as a means of providing a conforming environment for
191 the child process, it is not possible to standardize an interface that
192 closes arbitrary file descriptors above a certain value while still
193 guaranteeing a conforming environment.
194
196 None.
197
199 Section 2.6, STREAMS, exec, fattach(), fclose(), fdetach(), fopen(),
200 ioctl(), open(), unlink()
201
202 The Base Definitions volume of POSIX.1‐2008, <unistd.h>
203
205 Portions of this text are reprinted and reproduced in electronic form
206 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
207 -- Portable Operating System Interface (POSIX), The Open Group Base
208 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
209 cal and Electronics Engineers, Inc and The Open Group. (This is
210 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
211 event of any discrepancy between this version and the original IEEE and
212 The Open Group Standard, the original IEEE and The Open Group Standard
213 is the referee document. The original Standard can be obtained online
214 at http://www.unix.org/online.html .
215
216 Any typographical or formatting errors that appear in this page are
217 most likely to have been introduced during the conversion of the source
218 files to man page format. To report such errors, see https://www.ker‐
219 nel.org/doc/man-pages/reporting_bugs.html .
220
221
222
223IEEE/The Open Group 2013 CLOSE(3P)