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