OSDN Git Service

LDP: Update original to LDP v3.79
[linuxjm/LDP_man-pages.git] / original / man2 / close.2
1 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
2 .\"             and Copyright (C) 1993 Michael Haardt, Ian Jackson.
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein.  The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .\" Modified Wed Jul 21 22:40:25 1993 by Rik Faith <faith@cs.unc.edu>
27 .\" Modified Sat Feb 18 15:27:48 1995 by Michael Haardt
28 .\" Modified Sun Apr 14 11:40:50 1996 by Andries Brouwer <aeb@cwi.nl>:
29 .\"   corrected description of effect on locks (thanks to
30 .\"   Tigran Aivazian <tigran@sco.com>).
31 .\" Modified Fri Jan 31 16:21:46 1997 by Eric S. Raymond <esr@thyrsus.com>
32 .\" Modified 2000-07-22 by Nicolás Lichtmaier <nick@debian.org>
33 .\"   added note about close(2) not guaranteeing that data is safe on close.
34 .\"
35 .TH CLOSE 2 2013-12-30 "Linux" "Linux Programmer's Manual"
36 .SH NAME
37 close \- close a file descriptor
38 .SH SYNOPSIS
39 .nf
40 .B #include <unistd.h>
41 .sp
42 .BI "int close(int " fd );
43 .fi
44 .SH DESCRIPTION
45 .BR close ()
46 closes a file descriptor, so that it no longer refers to any file and
47 may be reused.
48 Any record locks (see
49 .BR fcntl (2))
50 held on the file it was associated with,
51 and owned by the process, are removed (regardless of the file
52 descriptor that was used to obtain the lock).
53 .PP
54 If
55 .I fd
56 is the last file descriptor referring to the underlying
57 open file description (see
58 .BR open (2)),
59 the resources associated with the open file description are freed;
60 if the descriptor was the last reference to a file which has been
61 removed using
62 .BR unlink (2),
63 the file is deleted.
64 .SH RETURN VALUE
65 .BR close ()
66 returns zero on success.
67 On error, \-1 is returned, and
68 .I errno
69 is set appropriately.
70 .SH ERRORS
71 .TP
72 .B EBADF
73 .I fd
74 isn't a valid open file descriptor.
75 .TP
76 .B EINTR
77 The
78 .BR close ()
79 call was interrupted by a signal; see
80 .BR signal (7).
81 .TP
82 .B EIO
83 An I/O error occurred.
84 .SH CONFORMING TO
85 SVr4, 4.3BSD, POSIX.1-2001.
86 .\" SVr4 documents an additional ENOLINK error condition.
87 .SH NOTES
88 Not checking the return value of
89 .BR close ()
90 is a common but nevertheless
91 serious programming error.
92 It is quite possible that errors on a
93 previous
94 .BR write (2)
95 operation are first reported at the final
96 .BR close ().
97 Not checking the return value when closing the file may lead to
98 silent loss of data.
99 This can especially be observed with NFS
100 and with disk quota.
101 Note that the return value should be used only for diagnostics.
102 In particular
103 .BR close ()
104 should not be retried after an
105 .B EINTR
106 since this may cause a reused descriptor from another thread to be closed.
107 .PP
108 A successful close does not guarantee that the data has been successfully
109 saved to disk, as the kernel defers writes.
110 It is not common for a filesystem
111 to flush the buffers when the stream is closed.
112 If you need to be sure that
113 the data is physically stored, use
114 .BR fsync (2).
115 (It will depend on the disk hardware at this point.)
116 .PP
117 It is probably unwise to close file descriptors while
118 they may be in use by system calls in
119 other threads in the same process.
120 Since a file descriptor may be reused,
121 there are some obscure race conditions
122 that may cause unintended side effects.
123 .\" Date: Tue, 4 Sep 2007 13:57:35 +0200
124 .\" From: Fredrik Noring <noring@nocrew.org>
125 .\" One such race involves signals and ERESTARTSYS. If a file descriptor
126 .\" in use by a system call is closed and then reused by e.g. an
127 .\" independent open() in some unrelated thread, before the original system
128 .\" call has restarted after ERESTARTSYS, the original system call will
129 .\" later restart with the reused file descriptor. This is most likely a
130 .\" serious programming error.
131 .SH SEE ALSO
132 .BR fcntl (2),
133 .BR fsync (2),
134 .BR open (2),
135 .BR shutdown (2),
136 .BR unlink (2),
137 .BR fclose (3)
138 .SH COLOPHON
139 This page is part of release 3.79 of the Linux
140 .I man-pages
141 project.
142 A description of the project,
143 information about reporting bugs,
144 and the latest version of this page,
145 can be found at
146 \%http://www.kernel.org/doc/man\-pages/.