Blame man2/close.2

Packit 7cfc04
.\" This manpage is Copyright (C) 1992 Drew Eckhardt;
Packit 7cfc04
.\" and Copyright (C) 1993 Michael Haardt, Ian Jackson.
Packit 7cfc04
.\" and Copyright (C) 2016 Michael Kerrisk <mtk.manpages@gmail.com>
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(VERBATIM)
Packit 7cfc04
.\" Permission is granted to make and distribute verbatim copies of this
Packit 7cfc04
.\" manual provided the copyright notice and this permission notice are
Packit 7cfc04
.\" preserved on all copies.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Permission is granted to copy and distribute modified versions of this
Packit 7cfc04
.\" manual under the conditions for verbatim copying, provided that the
Packit 7cfc04
.\" entire resulting derived work is distributed under the terms of a
Packit 7cfc04
.\" permission notice identical to this one.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Since the Linux kernel and libraries are constantly changing, this
Packit 7cfc04
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
Packit 7cfc04
.\" responsibility for errors or omissions, or for damages resulting from
Packit 7cfc04
.\" the use of the information contained herein.  The author(s) may not
Packit 7cfc04
.\" have taken the same level of care in the production of this manual,
Packit 7cfc04
.\" which is licensed free of charge, as they might when working
Packit 7cfc04
.\" professionally.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Formatted or processed versions of this manual, if unaccompanied by
Packit 7cfc04
.\" the source, must acknowledge the copyright and authors of this work.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.\" Modified Wed Jul 21 22:40:25 1993 by Rik Faith <faith@cs.unc.edu>
Packit 7cfc04
.\" Modified Sat Feb 18 15:27:48 1995 by Michael Haardt
Packit 7cfc04
.\" Modified Sun Apr 14 11:40:50 1996 by Andries Brouwer <aeb@cwi.nl>:
Packit 7cfc04
.\"   corrected description of effect on locks (thanks to
Packit 7cfc04
.\"   Tigran Aivazian <tigran@sco.com>).
Packit 7cfc04
.\" Modified Fri Jan 31 16:21:46 1997 by Eric S. Raymond <esr@thyrsus.com>
Packit 7cfc04
.\" Modified 2000-07-22 by Nicolás Lichtmaier <nick@debian.org>
Packit 7cfc04
.\"   added note about close(2) not guaranteeing that data is safe on close.
Packit 7cfc04
.\"
Packit 7cfc04
.TH CLOSE 2 2017-09-15 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
close \- close a file descriptor
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.nf
Packit 7cfc04
.B #include <unistd.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "int close(int " fd );
Packit 7cfc04
.fi
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
.BR close ()
Packit 7cfc04
closes a file descriptor, so that it no longer refers to any file and
Packit 7cfc04
may be reused.
Packit 7cfc04
Any record locks (see
Packit 7cfc04
.BR fcntl (2))
Packit 7cfc04
held on the file it was associated with,
Packit 7cfc04
and owned by the process, are removed (regardless of the file
Packit 7cfc04
descriptor that was used to obtain the lock).
Packit 7cfc04
.PP
Packit 7cfc04
If
Packit 7cfc04
.I fd
Packit 7cfc04
is the last file descriptor referring to the underlying
Packit 7cfc04
open file description (see
Packit 7cfc04
.BR open (2)),
Packit 7cfc04
the resources associated with the open file description are freed;
Packit 7cfc04
if the file descriptor was the last reference to a file which has been
Packit 7cfc04
removed using
Packit 7cfc04
.BR unlink (2),
Packit 7cfc04
the file is deleted.
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
.BR close ()
Packit 7cfc04
returns zero on success.
Packit 7cfc04
On error, \-1 is returned, and
Packit 7cfc04
.I errno
Packit 7cfc04
is set appropriately.
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
.TP
Packit 7cfc04
.B EBADF
Packit 7cfc04
.I fd
Packit 7cfc04
isn't a valid open file descriptor.
Packit 7cfc04
.TP
Packit 7cfc04
.B EINTR
Packit 7cfc04
The
Packit 7cfc04
.BR close ()
Packit 7cfc04
call was interrupted by a signal; see
Packit 7cfc04
.BR signal (7).
Packit 7cfc04
.TP
Packit 7cfc04
.B EIO
Packit 7cfc04
An I/O error occurred.
Packit 7cfc04
.TP
Packit 7cfc04
.BR ENOSPC ", " EDQUOT
Packit 7cfc04
On NFS, these errors are not normally reported against the first write
Packit 7cfc04
which exceeds the available storage space, but instead against a
Packit 7cfc04
subsequent
Packit 7cfc04
.BR write (2),
Packit 7cfc04
.BR fsync (2),
Packit 7cfc04
or
Packit 7cfc04
.BR close (2).
Packit 7cfc04
.PP
Packit 7cfc04
See NOTES for a discussion of why
Packit 7cfc04
.BR close ()
Packit 7cfc04
should not be retried after an error.
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
Packit 7cfc04
.\" SVr4 documents an additional ENOLINK error condition.
Packit 7cfc04
.SH NOTES
Packit 7cfc04
A successful close does not guarantee that the data has been successfully
Packit 7cfc04
saved to disk, as the kernel uses the buffer cache to defer writes.
Packit 7cfc04
Typically, filesystems do not flush buffers when a file is closed.
Packit 7cfc04
If you need to be sure that
Packit 7cfc04
the data is physically stored on the underlying disk, use
Packit 7cfc04
.BR fsync (2).
Packit 7cfc04
(It will depend on the disk hardware at this point.)
Packit 7cfc04
.PP
Packit 7cfc04
The close-on-exec file descriptor flag can be used to ensure
Packit 7cfc04
that a file descriptor is automatically closed upon a successful
Packit 7cfc04
.BR execve (2);
Packit 7cfc04
see
Packit 7cfc04
.BR fcntl (2)
Packit 7cfc04
for details.
Packit 7cfc04
.PP
Packit 7cfc04
It is probably unwise to close file descriptors while
Packit 7cfc04
they may be in use by system calls in
Packit 7cfc04
other threads in the same process.
Packit 7cfc04
Since a file descriptor may be reused,
Packit 7cfc04
there are some obscure race conditions
Packit 7cfc04
that may cause unintended side effects.
Packit 7cfc04
.\" Date: Tue, 4 Sep 2007 13:57:35 +0200
Packit 7cfc04
.\" From: Fredrik Noring <noring@nocrew.org>
Packit 7cfc04
.\" One such race involves signals and ERESTARTSYS. If a file descriptor
Packit 7cfc04
.\" in use by a system call is closed and then reused by e.g. an
Packit 7cfc04
.\" independent open() in some unrelated thread, before the original system
Packit 7cfc04
.\" call has restarted after ERESTARTSYS, the original system call will
Packit 7cfc04
.\" later restart with the reused file descriptor. This is most likely a
Packit 7cfc04
.\" serious programming error.
Packit 7cfc04
.\"
Packit 7cfc04
.SS Dealing with error returns from close()
Packit 7cfc04
A careful programmer will check the return value of
Packit 7cfc04
.BR close (),
Packit 7cfc04
since it is quite possible that errors on a previous
Packit 7cfc04
.BR write (2)
Packit 7cfc04
operation are reported only on the final
Packit 7cfc04
.BR close ()
Packit 7cfc04
that releases the open file description.
Packit 7cfc04
Failing to check the return value when closing a file may lead to
Packit 7cfc04
.I silent
Packit 7cfc04
loss of data.
Packit 7cfc04
This can especially be observed with NFS and with disk quota.
Packit 7cfc04
.PP
Packit 7cfc04
Note, however, that a failure return should be used only for
Packit 7cfc04
diagnostic purposes (i.e., a warning to the application that there
Packit 7cfc04
may still be I/O pending or there may have been failed I/O)
Packit 7cfc04
or remedial purposes
Packit 7cfc04
(e.g., writing the file once more or creating a backup).
Packit 7cfc04
.PP
Packit 7cfc04
Retrying the
Packit 7cfc04
.BR close ()
Packit 7cfc04
after a failure return is the wrong thing to do,
Packit 7cfc04
.\" The file descriptor is released early in close();
Packit 7cfc04
.\" close() ==> __close_fd():
Packit 7cfc04
.\"			__put_unused_fd() ==> __clear_open_fd()
Packit 7cfc04
.\"			return filp_close(file, files);
Packit 7cfc04
.\"
Packit 7cfc04
.\" The errors are returned by filp_close() after the FD has been
Packit 7cfc04
.\" cleared for re-use.
Packit 7cfc04
since this may cause a reused file descriptor
Packit 7cfc04
from another thread to be closed.
Packit 7cfc04
This can occur because the Linux kernel
Packit 7cfc04
.I always
Packit 7cfc04
releases the file descriptor early in the close
Packit 7cfc04
operation, freeing it for reuse;
Packit 7cfc04
the steps that may return an error,
Packit 7cfc04
.\" filp_close()
Packit 7cfc04
such as flushing data to the filesystem or device,
Packit 7cfc04
occur only later in the close operation.
Packit 7cfc04
.PP
Packit 7cfc04
Many other implementations similarly always close the file descriptor
Packit 7cfc04
.\" FreeBSD documents this explicitly. From the look of the source code
Packit 7cfc04
.\" SVR4, ancient SunOS, later Solaris, and AIX all do this.
Packit 7cfc04
(except in the case of
Packit 7cfc04
.BR EBADF ,
Packit 7cfc04
meaning that the file descriptor was invalid)
Packit 7cfc04
even if they subsequently report an error on return from
Packit 7cfc04
.BR close ().
Packit 7cfc04
POSIX.1 is currently silent on this point,
Packit 7cfc04
but there are plans to mandate this behavior in the next major release
Packit 7cfc04
.\" Issue 8
Packit 7cfc04
of the standard
Packit 7cfc04
.PP
Packit 7cfc04
A careful programmer who wants to know about I/O errors may precede
Packit 7cfc04
.BR close ()
Packit 7cfc04
with a call to
Packit 7cfc04
.BR fsync (2).
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.B EINTR
Packit 7cfc04
error is a somewhat special case.
Packit 7cfc04
Regarding the
Packit 7cfc04
.B EINTR
Packit 7cfc04
error, POSIX.1-2013 says:
Packit 7cfc04
.PP
Packit 7cfc04
.RS
Packit 7cfc04
If
Packit 7cfc04
.BR close ()
Packit 7cfc04
is interrupted by a signal that is to be caught, it shall return \-1 with
Packit 7cfc04
.I errno
Packit 7cfc04
set to
Packit 7cfc04
.B EINTR
Packit 7cfc04
and the state of
Packit 7cfc04
.I fildes
Packit 7cfc04
is unspecified.
Packit 7cfc04
.RE
Packit 7cfc04
.PP
Packit 7cfc04
This permits the behavior that occurs on Linux and
Packit 7cfc04
many other implementations, where,
Packit 7cfc04
as with other errors that may be reported by
Packit 7cfc04
.BR close (),
Packit 7cfc04
the file descriptor is guaranteed to be closed.
Packit 7cfc04
However, it also permits another possibility:
Packit 7cfc04
that the implementation returns an
Packit 7cfc04
.B EINTR
Packit 7cfc04
error and keeps the file descriptor open.
Packit 7cfc04
(According to its documentation, HP-UX's
Packit 7cfc04
.BR close ()
Packit 7cfc04
does this.)
Packit 7cfc04
The caller must then once more use
Packit 7cfc04
.BR close ()
Packit 7cfc04
to close the file descriptor, to avoid file descriptor leaks.
Packit 7cfc04
This divergence in implementation behaviors provides
Packit 7cfc04
a difficult hurdle for portable applications, since on many implementations,
Packit 7cfc04
.BR close ()
Packit 7cfc04
must not be called again after an
Packit 7cfc04
.B EINTR
Packit 7cfc04
error, and on at least one,
Packit 7cfc04
.BR close ()
Packit 7cfc04
must be called again.
Packit 7cfc04
There are plans to address this conundrum for
Packit 7cfc04
the next major release of the POSIX.1 standard.
Packit 7cfc04
.\" FIXME . for later review when Issue 8 is one day released...
Packit 7cfc04
.\" POSIX proposes further changes for EINTR
Packit 7cfc04
.\" http://austingroupbugs.net/tag_view_page.php?tag_id=8
Packit 7cfc04
.\" http://austingroupbugs.net/view.php?id=529
Packit 7cfc04
.\"
Packit 7cfc04
.\" FIXME .
Packit 7cfc04
.\" Review the following glibc bug later
Packit 7cfc04
.\" https://sourceware.org/bugzilla/show_bug.cgi?id=14627
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR fcntl (2),
Packit 7cfc04
.BR fsync (2),
Packit 7cfc04
.BR open (2),
Packit 7cfc04
.BR shutdown (2),
Packit 7cfc04
.BR unlink (2),
Packit 7cfc04
.BR fclose (3)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.