1CLOSE(P)                   POSIX Programmer's Manual                  CLOSE(P)
2
3
4

NAME

6       close - close a file descriptor
7

SYNOPSIS

9       #include <unistd.h>
10
11       int close(int fildes);
12
13

DESCRIPTION

15       The  close() function shall deallocate the file descriptor indicated by
16       fildes. To deallocate means to make the file descriptor  available  for
17       return  by  subsequent calls to open() or other functions that allocate
18       file descriptors. All outstanding record locks owned by the process  on
19       the file associated with the file descriptor shall be removed (that is,
20       unlocked).
21
22       If close() is interrupted by a signal that is to be  caught,  it  shall
23       return -1 with errno set to [EINTR] and the state of fildes is unspeci‐
24       fied. If an I/O error occurred while reading from  or  writing  to  the
25       file  system  during close(), it may return -1 with errno set to [EIO];
26       if this error is returned, the state of fildes is unspecified.
27
28       When all file descriptors associated with a pipe or FIFO  special  file
29       are closed, any data remaining in the pipe or FIFO shall be discarded.
30
31       When all file descriptors associated with an open file description have
32       been closed, the open file description shall be freed.
33
34       If the link count of the file is 0, when all file  descriptors  associ‐
35       ated  with the file are closed, the space occupied by the file shall be
36       freed and the file shall no longer be accessible.
37
38       If a STREAMS-based fildes is closed and the calling process was  previ‐
39       ously registered to receive a SIGPOLL signal for events associated with
40       that STREAM, the calling process shall be unregistered for events asso‐
41       ciated  with  the STREAM. The last close() for a STREAM shall cause the
42       STREAM associated with fildes to be dismantled. If  O_NONBLOCK  is  not
43       set  and there have been no signals posted for the STREAM, and if there
44       is data on the module's write queue, close() shall wait for an unspeci‐
45       fied  time  (for each module and driver) for any output to drain before
46       dismantling the STREAM. The time delay can be  changed  via  an  I_SET‐
47       CLTIME  ioctl() request. If the O_NONBLOCK flag is set, or if there are
48       any pending signals, close() shall not wait for output  to  drain,  and
49       shall dismantle the STREAM immediately.
50
51       If the implementation supports STREAMS-based pipes, and fildes is asso‐
52       ciated with one end of a pipe, the last close() shall cause a hangup to
53       occur  on  the  other end of the pipe. In addition, if the other end of
54       the pipe has been named by fattach(), then the last close() shall force
55       the named end to be detached by fdetach(). If the named end has no open
56       file descriptors associated with it and gets detached, the STREAM asso‐
57       ciated with that end shall also be dismantled.
58
59       If  fildes  refers to the master side of a pseudo-terminal, and this is
60       the last close, a SIGHUP  signal  shall  be  sent  to  the  controlling
61       process, if any, for which the slave side of the pseudo-terminal is the
62       controlling terminal. It is unspecified whether closing the master side
63       of the pseudo-terminal flushes all queued input and output.
64
65       If  fildes refers to the slave side of a STREAMS-based pseudo-terminal,
66       a zero-length message may be sent to the master.
67
68       When there is an  outstanding  cancelable  asynchronous  I/O  operation
69       against  fildes  when close() is called, that I/O operation may be can‐
70       celed. An I/O operation that  is  not  canceled  completes  as  if  the
71       close()  operation  had  not yet occurred.  All operations that are not
72       canceled shall complete as if the close() blocked until the  operations
73       completed.  The  close()  operation itself need not block awaiting such
74       I/O completion.  Whether any I/O operation is canceled, and  which  I/O
75       operation may be canceled upon close(), is implementation-defined.
76
77       If a shared memory object or a memory mapped file remains referenced at
78       the last close (that is, a process has it mapped), then the entire con‐
79       tents  of  the  memory  object  shall  persist  until the memory object
80       becomes unreferenced. If this is the last  close  of  a  shared  memory
81       object  or  a  memory  mapped  file and the close results in the memory
82       object becoming unreferenced, and the memory object has been  unlinked,
83       then the memory object shall be removed.
84
85       If  fildes  refers  to  a  socket, close() shall cause the socket to be
86       destroyed. If the socket  is  in  connection-mode,  and  the  SO_LINGER
87       option  is set for the socket with non-zero linger time, and the socket
88       has untransmitted data, then close() shall block for up to the  current
89       linger interval until all data is transmitted.
90

RETURN VALUE

92       Upon successful completion, 0 shall be returned; otherwise, -1 shall be
93       returned and errno set to indicate the error.
94

ERRORS

96       The close() function shall fail if:
97
98       EBADF  The fildes argument is not a valid file descriptor.
99
100       EINTR  The close() function was interrupted by a signal.
101
102
103       The close() function may fail if:
104
105       EIO    An I/O error occurred while reading from or writing to the  file
106              system.
107
108
109       The following sections are informative.
110

EXAMPLES

112   Reassigning a File Descriptor
113       The  following example closes the file descriptor associated with stan‐
114       dard output for the current process, re-assigns standard  output  to  a
115       new  file  descriptor, and closes the original file descriptor to clean
116       up. This example assumes that the  file  descriptor  0  (which  is  the
117       descriptor for standard input) is not closed.
118
119
120              #include <unistd.h>
121              ...
122              int pfd;
123              ...
124              close(1);
125              dup(pfd);
126              close(pfd);
127              ...
128
129       Incidentally, this is exactly what could be achieved using:
130
131
132              dup2(pfd, 1);
133              close(pfd);
134
135   Closing a File Descriptor
136       In  the  following  example, close() is used to close a file descriptor
137       after an unsuccessful attempt is made to associate that file descriptor
138       with a stream.
139
140
141              #include <stdio.h>
142              #include <unistd.h>
143              #include <stdlib.h>
144
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

APPLICATION USAGE

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

RATIONALE

165       The use of interruptible device close routines should be discouraged to
166       avoid problems with the implicit closes of file descriptors by exec and
167       exit(). This volume of IEEE Std 1003.1-2001 only intends to permit such
168       behavior by specifying the [EINTR] error condition.
169

FUTURE DIRECTIONS

171       None.
172

SEE ALSO

174       STREAMS , fattach() , fclose() , fdetach() , fopen() , ioctl() , open()
175       , the Base Definitions volume of IEEE Std 1003.1-2001, <unistd.h>
176
178       Portions of this text are reprinted and reproduced in  electronic  form
179       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
180       -- Portable Operating System Interface (POSIX),  The  Open  Group  Base
181       Specifications  Issue  6,  Copyright  (C) 2001-2003 by the Institute of
182       Electrical and Electronics Engineers, Inc and The Open  Group.  In  the
183       event of any discrepancy between this version and the original IEEE and
184       The Open Group Standard, the original IEEE and The Open Group  Standard
185       is  the  referee document. The original Standard can be obtained online
186       at http://www.opengroup.org/unix/online.html .
187
188
189
190IEEE/The Open Group                  2003                             CLOSE(P)
Impressum