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