]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/read.2
open.2, read.2, write.2: Clarify that O_NONBLOCK is a no-op for regular files and...
[thirdparty/man-pages.git] / 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 See
104 .BR open (2)
105 for further details on the
106 .BR O_NONBLOCK
107 flag.
108 .TP
109 .BR EAGAIN " or " EWOULDBLOCK
110 .\" Actually EAGAIN on Linux
111 The file descriptor
112 .I fd
113 refers to a socket and has been marked nonblocking
114 .RB ( O_NONBLOCK ),
115 and the read would block.
116 POSIX.1-2001 allows either error to be returned for this case,
117 and does not require these constants to have the same value,
118 so a portable application should check for both possibilities.
119 .TP
120 .B EBADF
121 .I fd
122 is not a valid file descriptor or is not open for reading.
123 .TP
124 .B EFAULT
125 .I buf
126 is outside your accessible address space.
127 .TP
128 .B EINTR
129 The call was interrupted by a signal before any data was read; see
130 .BR signal (7).
131 .TP
132 .B EINVAL
133 .I fd
134 is attached to an object which is unsuitable for reading;
135 or the file was opened with the
136 .B O_DIRECT
137 flag, and either the address specified in
138 .IR buf ,
139 the value specified in
140 .IR count ,
141 or the current file offset is not suitably aligned.
142 .TP
143 .B EINVAL
144 .I fd
145 was created via a call to
146 .BR timerfd_create (2)
147 and the wrong size buffer was given to
148 .BR read ();
149 see
150 .BR timerfd_create (2)
151 for further information.
152 .TP
153 .B EIO
154 I/O error.
155 This will happen for example when the process is in a
156 background process group, tries to read from its controlling terminal,
157 and either it is ignoring or blocking
158 .B SIGTTIN
159 or its process group
160 is orphaned.
161 It may also occur when there is a low-level I/O error
162 while reading from a disk or tape.
163 .TP
164 .B EISDIR
165 .I fd
166 refers to a directory.
167 .PP
168 Other errors may occur, depending on the object connected to
169 .IR fd .
170 POSIX allows a
171 .BR read ()
172 that is interrupted after reading some data
173 to return \-1 (with
174 .I errno
175 set to
176 .BR EINTR )
177 or to return the number of bytes already read.
178 .SH CONFORMING TO
179 SVr4, 4.3BSD, POSIX.1-2001.
180 .SH NOTES
181 On NFS filesystems, reading small amounts of data will update the
182 timestamp only the first time, subsequent calls may not do so.
183 This is caused
184 by client side attribute caching, because most if not all NFS clients
185 leave
186 .I st_atime
187 (last file access time)
188 updates to the server, and client side reads satisfied from the
189 client's cache will not cause
190 .I st_atime
191 updates on the server as there are no
192 server-side reads.
193 UNIX semantics can be obtained by disabling client-side attribute caching,
194 but in most situations this will substantially
195 increase server load and decrease performance.
196 .SH BUGS
197 According to POSIX.1-2008/SUSv4 Section XSI 2.9.7
198 ("Thread Interactions with Regular File Operations"):
199
200 .RS 4
201 All of the following functions shall be atomic with respect to
202 each other in the effects specified in POSIX.1-2008 when they
203 operate on regular files or symbolic links: ...
204 .RE
205
206 Among the APIs subsequently listed are
207 .BR read ()
208 and
209 .BR readv (2).
210 And among the effects that should be atomic across threads (and processes)
211 are updates of the file offset.
212 However, on Linux before version 3.14,
213 this was not the case: if two processes that share
214 an open file description (see
215 .BR open (2))
216 perform a
217 .BR read ()
218 (or
219 .BR readv (2))
220 at the same time, then the I/O operations were not atomic
221 with respect updating the file offset,
222 with the result that the reads in the two processes
223 might (incorrectly) overlap in the blocks of data that they obtained.
224 This problem was fixed in Linux 3.14.
225 .\" http://thread.gmane.org/gmane.linux.kernel/1649458
226 .\" From: Michael Kerrisk (man-pages <mtk.manpages <at> gmail.com>
227 .\" Subject: Update of file offset on write() etc. is non-atomic with I/O
228 .\" Date: 2014-02-17 15:41:37 GMT
229 .\" Newsgroups: gmane.linux.kernel, gmane.linux.file-systems
230 .\" commit 9c225f2655e36a470c4f58dbbc99244c5fc7f2d4
231 .\" Author: Linus Torvalds <torvalds@linux-foundation.org>
232 .\" Date: Mon Mar 3 09:36:58 2014 -0800
233 .\"
234 .\" vfs: atomic f_pos accesses as per POSIX
235 .SH SEE ALSO
236 .BR close (2),
237 .BR fcntl (2),
238 .BR ioctl (2),
239 .BR lseek (2),
240 .BR open (2),
241 .BR pread (2),
242 .BR readdir (2),
243 .BR readlink (2),
244 .BR readv (2),
245 .BR select (2),
246 .BR write (2),
247 .BR fread (3)