OSDN Git Service

LDP: Update original to LDP v3.79
[linuxjm/LDP_man-pages.git] / original / man2 / read.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 Sat Jul 24 00:06:00 1993 by Rik Faith <faith@cs.unc.edu>
27 .\" Modified Wed Jan 17 16:02:32 1996 by Michael Haardt
28 .\"   <michael@cantor.informatik.rwth-aachen.de>
29 .\" Modified Thu Apr 11 19:26:35 1996 by Andries Brouwer <aeb@cwi.nl>
30 .\" Modified Sun Jul 21 18:59:33 1996 by Andries Brouwer <aeb@cwi.nl>
31 .\" Modified Fri Jan 31 16:47:33 1997 by Eric S. Raymond <esr@thyrsus.com>
32 .\" Modified Sat Jul 12 20:45:39 1997 by Michael Haardt
33 .\"   <michael@cantor.informatik.rwth-aachen.de>
34 .\"
35 .TH READ 2 2014-05-04 "Linux" "Linux Programmer's Manual"
36 .SH NAME
37 read \- read from a file descriptor
38 .SH SYNOPSIS
39 .nf
40 .B #include <unistd.h>
41 .sp
42 .BI "ssize_t read(int " fd ", void *" buf ", size_t " count );
43 .fi
44 .SH DESCRIPTION
45 .BR read ()
46 attempts to read up to
47 .I count
48 bytes from file descriptor
49 .I fd
50 into the buffer starting at
51 .IR buf .
52
53 On files that support seeking,
54 the read operation commences at the current file offset,
55 and the file offset is incremented by the number of bytes read.
56 If the current file offset is at or past the end of file,
57 no bytes are read, and
58 .BR read ()
59 returns zero.
60
61 If
62 .I count
63 is zero,
64 .BR read ()
65 .I may
66 detect the errors described below.
67 In the absence of any errors,
68 or if
69 .BR read ()
70 does not check for errors, a
71 .BR read ()
72 with a
73 .I count
74 of 0 returns zero and has no other effects.
75
76 If
77 .I count
78 is greater than
79 .BR SSIZE_MAX ,
80 the result is unspecified.
81 .SH RETURN VALUE
82 On success, the number of bytes read is returned (zero indicates end of
83 file), and the file position is advanced by this number.
84 It is not an error if this number is smaller than the number of bytes
85 requested; this may happen for example because fewer bytes are actually
86 available right now (maybe because we were close to end-of-file, or
87 because we are reading from a pipe, or from a terminal), or because
88 .BR read ()
89 was interrupted by a signal.
90 On error, \-1 is returned, and
91 .I errno
92 is set appropriately.
93 In this case, it is left unspecified whether
94 the file position (if any) changes.
95 .SH ERRORS
96 .TP
97 .B EAGAIN
98 The file descriptor
99 .I fd
100 refers to a file other than a socket and has been marked nonblocking
101 .RB ( O_NONBLOCK ),
102 and the read would block.
103 .TP
104 .BR EAGAIN " or " EWOULDBLOCK
105 .\" Actually EAGAIN on Linux
106 The file descriptor
107 .I fd
108 refers to a socket and has been marked nonblocking
109 .RB ( O_NONBLOCK ),
110 and the read would block.
111 POSIX.1-2001 allows either error to be returned for this case,
112 and does not require these constants to have the same value,
113 so a portable application should check for both possibilities.
114 .TP
115 .B EBADF
116 .I fd
117 is not a valid file descriptor or is not open for reading.
118 .TP
119 .B EFAULT
120 .I buf
121 is outside your accessible address space.
122 .TP
123 .B EINTR
124 The call was interrupted by a signal before any data was read; see
125 .BR signal (7).
126 .TP
127 .B EINVAL
128 .I fd
129 is attached to an object which is unsuitable for reading;
130 or the file was opened with the
131 .B O_DIRECT
132 flag, and either the address specified in
133 .IR buf ,
134 the value specified in
135 .IR count ,
136 or the current file offset is not suitably aligned.
137 .TP
138 .B EINVAL
139 .I fd
140 was created via a call to
141 .BR timerfd_create (2)
142 and the wrong size buffer was given to
143 .BR read ();
144 see
145 .BR timerfd_create (2)
146 for further information.
147 .TP
148 .B EIO
149 I/O error.
150 This will happen for example when the process is in a
151 background process group, tries to read from its controlling terminal,
152 and either it is ignoring or blocking
153 .B SIGTTIN
154 or its process group
155 is orphaned.
156 It may also occur when there is a low-level I/O error
157 while reading from a disk or tape.
158 .TP
159 .B EISDIR
160 .I fd
161 refers to a directory.
162 .PP
163 Other errors may occur, depending on the object connected to
164 .IR fd .
165 POSIX allows a
166 .BR read ()
167 that is interrupted after reading some data
168 to return \-1 (with
169 .I errno
170 set to
171 .BR EINTR )
172 or to return the number of bytes already read.
173 .SH CONFORMING TO
174 SVr4, 4.3BSD, POSIX.1-2001.
175 .SH NOTES
176 On NFS filesystems, reading small amounts of data will update the
177 timestamp only the first time, subsequent calls may not do so.
178 This is caused
179 by client side attribute caching, because most if not all NFS clients
180 leave st_atime (last file access time)
181 updates to the server and client side reads satisfied from the
182 client's cache will not cause st_atime updates on the server as there are no
183 server side reads.
184 UNIX semantics can be obtained by disabling client
185 side attribute caching, but in most situations this will substantially
186 increase server load and decrease performance.
187 .SH BUGS
188 According to POSIX.1-2008/SUSv4 Section XSI 2.9.7
189 ("Thread Interactions with Regular File Operations"):
190
191 .RS 4
192 All of the following functions shall be atomic with respect to
193 each other in the effects specified in POSIX.1-2008 when they
194 operate on regular files or symbolic links: ...
195 .RE
196
197 Among the APIs subsequently listed are
198 .BR read ()
199 and
200 .BR readv (2).
201 And among the effects that should be atomic across threads (and processes)
202 are updates of the file offset.
203 However, on Linux before version 3.14,
204 this was not the case: if two processes that share
205 an open file description (see
206 .BR open (2))
207 perform a
208 .BR read ()
209 (or
210 .BR readv (2))
211 at the same time, then the I/O operations were not atomic
212 with respect updating the file offset,
213 with the result that the reads in the two processes
214 might (incorrectly) overlap in the blocks of data that they obtained.
215 This problem was fixed in Linux 3.14.
216 .\" http://thread.gmane.org/gmane.linux.kernel/1649458
217 .\"    From: Michael Kerrisk (man-pages <mtk.manpages <at> gmail.com>
218 .\"    Subject: Update of file offset on write() etc. is non-atomic with I/O
219 .\"    Date: 2014-02-17 15:41:37 GMT
220 .\"    Newsgroups: gmane.linux.kernel, gmane.linux.file-systems
221 .\" commit 9c225f2655e36a470c4f58dbbc99244c5fc7f2d4
222 .\"    Author: Linus Torvalds <torvalds@linux-foundation.org>
223 .\"    Date:   Mon Mar 3 09:36:58 2014 -0800
224 .\"
225 .\"        vfs: atomic f_pos accesses as per POSIX
226 .SH SEE ALSO
227 .BR close (2),
228 .BR fcntl (2),
229 .BR ioctl (2),
230 .BR lseek (2),
231 .BR open (2),
232 .BR pread (2),
233 .BR readdir (2),
234 .BR readlink (2),
235 .BR readv (2),
236 .BR select (2),
237 .BR write (2),
238 .BR fread (3)
239 .SH COLOPHON
240 This page is part of release 3.79 of the Linux
241 .I man-pages
242 project.
243 A description of the project,
244 information about reporting bugs,
245 and the latest version of this page,
246 can be found at
247 \%http://www.kernel.org/doc/man\-pages/.