CLOSE(3)                   POSIX Programmer's Manual                  CLOSE(3)



PPRROOLLOOGG
       This manual page is part of the POSIX Programmer's Manual.  The OS
       implementation of this interface may differ (consult the corresponding
       OS manual page for details of OS behavior), or the interface may not be
       implemented on OS.

NNAAMMEE
       close -- close a file descriptor

SSYYNNOOPPSSIISS
       #include <unistd.h>

       int close(int _f_i_l_d_e_s);

DDEESSCCRRIIPPTTIIOONN
       The _c_l_o_s_e() function shall deallocate the file descriptor indicated by
       _f_i_l_d_e_s.  To deallocate means to make the file descriptor available for
       return by subsequent calls to _o_p_e_n() or other functions that allocate
       file descriptors. All outstanding record locks owned by the process on
       the file associated with the file descriptor shall be removed (that is,
       unlocked).

       If _c_l_o_s_e() is interrupted by a signal that is to be caught, it shall
       return -1 with _e_r_r_n_o set to [[EEIINNTTRR]] and the state of _f_i_l_d_e_s is
       unspecified. If an I/O error occurred while reading from or writing to
       the file system during _c_l_o_s_e(), it may return -1 with _e_r_r_n_o set to
       [[EEIIOO]]; if this error is returned, the state of _f_i_l_d_e_s is unspecified.

       When all file descriptors associated with a pipe or FIFO special file
       are closed, any data remaining in the pipe or FIFO shall be discarded.

       When all file descriptors associated with an open file description have
       been closed, the open file description shall be freed.

       If the link count of the file is 0, when all file descriptors
       associated with the file are closed, the space occupied by the file
       shall be freed and the file shall no longer be accessible.

       If a STREAMS-based _f_i_l_d_e_s is closed and the calling process was
       previously registered to receive a SIGPOLL signal for events associated
       with that STREAM, the calling process shall be unregistered for events
       associated with the STREAM. The last _c_l_o_s_e() for a STREAM shall cause
       the STREAM associated with _f_i_l_d_e_s to be dismantled. If O_NONBLOCK is
       not set and there have been no signals posted for the STREAM, and if
       there is data on the module's write queue, _c_l_o_s_e() shall wait for an
       unspecified time (for each module and driver) for any output to drain
       before dismantling the STREAM. The time delay can be changed via an
       I_SETCLTIME _i_o_c_t_l() request. If the O_NONBLOCK flag is set, or if there
       are any pending signals, _c_l_o_s_e() shall not wait for output to drain,
       and shall dismantle the STREAM immediately.

       If the implementation supports STREAMS-based pipes, and _f_i_l_d_e_s is
       associated with one end of a pipe, the last _c_l_o_s_e() shall cause a
       hangup to occur on the other end of the pipe. In addition, if the other
       end of the pipe has been named by _f_a_t_t_a_c_h(), then the last _c_l_o_s_e()
       shall force the named end to be detached by _f_d_e_t_a_c_h().  If the named
       end has no open file descriptors associated with it and gets detached,
       the STREAM associated with that end shall also be dismantled.

       If _f_i_l_d_e_s refers to the master side of a pseudo-terminal, and this is
       the last close, a SIGHUP signal shall be sent to the controlling
       process, if any, for which the slave side of the pseudo-terminal is the
       controlling terminal. It is unspecified whether closing the master side
       of the pseudo-terminal flushes all queued input and output.

       If _f_i_l_d_e_s refers to the slave side of a STREAMS-based pseudo-terminal,
       a zero-length message may be sent to the master.

       When there is an outstanding cancelable asynchronous I/O operation
       against _f_i_l_d_e_s when _c_l_o_s_e() is called, that I/O operation may be
       canceled. An I/O operation that is not canceled completes as if the
       _c_l_o_s_e() operation had not yet occurred. All operations that are not
       canceled shall complete as if the _c_l_o_s_e() blocked until the operations
       completed. The _c_l_o_s_e() operation itself need not block awaiting such
       I/O completion. Whether any I/O operation is canceled, and which I/O
       operation may be canceled upon _c_l_o_s_e(), is implementation-defined.

       If a memory mapped file or a shared memory object remains referenced at
       the last close (that is, a process has it mapped), then the entire
       contents of the memory object shall persist until the memory object
       becomes unreferenced.  If this is the last close of a memory mapped
       file or a shared memory object and the close results in the memory
       object becoming unreferenced, and the memory object has been unlinked,
       then the memory object shall be removed.

       If _f_i_l_d_e_s refers to a socket, _c_l_o_s_e() shall cause the socket to be
       destroyed. If the socket is in connection-mode, and the SO_LINGER
       option is set for the socket with non-zero linger time, and the socket
       has untransmitted data, then _c_l_o_s_e() shall block for up to the current
       linger interval until all data is transmitted.

RREETTUURRNN VVAALLUUEE
       Upon successful completion, 0 shall be returned; otherwise, -1 shall be
       returned and _e_r_r_n_o set to indicate the error.

EERRRROORRSS
       The _c_l_o_s_e() function shall fail if:

       EEBBAADDFF  The _f_i_l_d_e_s argument is not a open file descriptor.

       EEIINNTTRR  The _c_l_o_s_e() function was interrupted by a signal.

       The _c_l_o_s_e() function may fail if:

       EEIIOO    An I/O error occurred while reading from or writing to the file
              system.

       _T_h_e _f_o_l_l_o_w_i_n_g _s_e_c_t_i_o_n_s _a_r_e _i_n_f_o_r_m_a_t_i_v_e_.

EEXXAAMMPPLLEESS
   RReeaassssiiggnniinngg aa FFiillee DDeessccrriippttoorr
       The following example closes the file descriptor associated with
       standard output for the current process, re-assigns standard output to
       a new file descriptor, and closes the original file descriptor to clean
       up. This example assumes that the file descriptor 0 (which is the
       descriptor for standard input) is not closed.


           #include <unistd.h>
           ...
           int pfd;
           ...
           close(1);
           dup(pfd);
           close(pfd);
           ...

       Incidentally, this is exactly what could be achieved using:


           dup2(pfd, 1);
           close(pfd);

   CClloossiinngg aa FFiillee DDeessccrriippttoorr
       In the following example, _c_l_o_s_e() is used to close a file descriptor
       after an unsuccessful attempt is made to associate that file descriptor
       with a stream.


           #include <stdio.h>
           #include <unistd.h>
           #include <stdlib.h>

           #define LOCKFILE "/etc/ptmp"
           ...
           int pfd;
           FILE *fpfd;
           ...
           if ((fpfd = fdopen (pfd, "w")) == NULL) {
               close(pfd);
               unlink(LOCKFILE);
               exit(1);
           }
           ...

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       An application that had used the _s_t_d_i_o routine _f_o_p_e_n() to open a file
       should use the corresponding _f_c_l_o_s_e() routine rather than _c_l_o_s_e().
       Once a file is closed, the file descriptor no longer exists, since the
       integer corresponding to it no longer refers to a file.

       Implementations may use file descriptors that must be inherited into
       child processes for the child process to remain conforming, such as for
       message catalog or tracing purposes. Therefore, an application that
       calls _c_l_o_s_e() on an arbitrary integer risks non-conforming behavior,
       and _c_l_o_s_e() can only portably be used on file descriptor values that
       the application has obtained through explicit actions, as well as the
       three file descriptors corresponding to the standard file streams. In
       multi-threaded parent applications, the practice of calling _c_l_o_s_e() in
       a loop after _f_o_r_k() and before an _e_x_e_c call in order to avoid a race
       condition of leaking an unintended file descriptor into a child
       process, is therefore unsafe, and the race should instead be combatted
       by opening all file descriptors with the FD_CLOEXEC bit set unless the
       file descriptor is intended to be inherited across _e_x_e_c.

       Usage of _c_l_o_s_e() on file descriptors STDIN_FILENO, STDOUT_FILENO, or
       STDERR_FILENO should immediately be followed by an operation to reopen
       these file descriptors. Unexpected behavior will result if any of these
       file descriptors is left in a closed state (for example, an [[EEBBAADDFF]]
       error from _p_e_r_r_o_r()) or if an unrelated _o_p_e_n() or similar call later in
       the application accidentally allocates a file to one of these well-
       known file descriptors. Furthermore, a _c_l_o_s_e() followed by a reopen
       operation (e.g., _o_p_e_n(), _d_u_p(), etc.) is not atomic; _d_u_p_2() should be
       used to change standard file descriptors.

RRAATTIIOONNAALLEE
       The use of interruptible device close routines should be discouraged to
       avoid problems with the implicit closes of file descriptors by _e_x_e_c and
       _e_x_i_t().  This volume of POSIX.1-2017 only intends to permit such
       behavior by specifying the [[EEIINNTTRR]] error condition.

       Note that the requirement for _c_l_o_s_e() on a socket to block for up to
       the current linger interval is not conditional on the O_NONBLOCK
       setting.

       The standard developers rejected a proposal to add _c_l_o_s_e_f_r_o_m() to the
       standard. Because the standard permits implementations to use inherited
       file descriptors as a means of providing a conforming environment for
       the child process, it is not possible to standardize an interface that
       closes arbitrary file descriptors above a certain value while still
       guaranteeing a conforming environment.

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       None.

SSEEEE AALLSSOO
       _S_e_c_t_i_o_n _2_._6, _S_T_R_E_A_M_S, _d_u_p(), _e_x_e_c, _e_x_i_t(), _f_a_t_t_a_c_h(), _f_c_l_o_s_e(),
       _f_d_e_t_a_c_h(), _f_o_p_e_n(), _f_o_r_k(), _i_o_c_t_l(), _o_p_e_n(), _p_e_r_r_o_r(), _u_n_l_i_n_k()

       The Base Definitions volume of POSIX.1-2017, <<uunniissttdd..hh>>

CCOOPPYYRRIIGGHHTT
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1-2017, Standard for Information Technology --
       Portable Operating System Interface (POSIX), The Open Group Base
       Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the
       Institute of Electrical and Electronics Engineers, Inc and The Open
       Group.  In the event of any discrepancy between this version and the
       original IEEE and The Open Group Standard, the original IEEE and The
       Open Group Standard is the referee document. The original Standard can
       be obtained online at http://www.opengroup.org/unix/online.html .

       Any typographical or formatting errors that appear in this page are
       most likely to have been introduced during the conversion of the source
       files to man page format. To report such errors, see
       https://www.kernel.org/doc/man-pages/reporting_bugs.html .



IEEE/The Open Group                  2017                             CLOSE(3)
