]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/read.2
on_exit.3: Atack variables may be out of scope when exit handler is invoked
[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 2018-02-02 "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 A further possible cause of
167 .B EIO
168 on networked filesystems is when an advisory lock had been taken
169 out on the file descriptor and this lock has been lost.
170 See the
171 .I "Lost locks"
172 section of
173 .BR fcntl (2)
174 for further details.
175 .TP
176 .B EISDIR
177 .I fd
178 refers to a directory.
179 .PP
180 Other errors may occur, depending on the object connected to
181 .IR fd .
182 .SH CONFORMING TO
183 SVr4, 4.3BSD, POSIX.1-2001.
184 .SH NOTES
185 The types
186 .I size_t
187 and
188 .I ssize_t
189 are, respectively,
190 unsigned and signed integer data types specified by POSIX.1.
191 .PP
192 On Linux,
193 .BR read ()
194 (and similar system calls) will transfer at most
195 0x7ffff000 (2,147,479,552) bytes,
196 returning the number of bytes actually transferred.
197 .\" commit e28cc71572da38a5a12c1cfe4d7032017adccf69
198 (This is true on both 32-bit and 64-bit systems.)
199 .PP
200 On NFS filesystems, reading small amounts of data will update the
201 timestamp only the first time, subsequent calls may not do so.
202 This is caused
203 by client side attribute caching, because most if not all NFS clients
204 leave
205 .I st_atime
206 (last file access time)
207 updates to the server, and client side reads satisfied from the
208 client's cache will not cause
209 .I st_atime
210 updates on the server as there are no
211 server-side reads.
212 UNIX semantics can be obtained by disabling client-side attribute caching,
213 but in most situations this will substantially
214 increase server load and decrease performance.
215 .SH BUGS
216 According to POSIX.1-2008/SUSv4 Section XSI 2.9.7
217 ("Thread Interactions with Regular File Operations"):
218 .PP
219 .RS 4
220 All of the following functions shall be atomic with respect to
221 each other in the effects specified in POSIX.1-2008 when they
222 operate on regular files or symbolic links: ...
223 .RE
224 .PP
225 Among the APIs subsequently listed are
226 .BR read ()
227 and
228 .BR readv (2).
229 And among the effects that should be atomic across threads (and processes)
230 are updates of the file offset.
231 However, on Linux before version 3.14,
232 this was not the case: if two processes that share
233 an open file description (see
234 .BR open (2))
235 perform a
236 .BR read ()
237 (or
238 .BR readv (2))
239 at the same time, then the I/O operations were not atomic
240 with respect updating the file offset,
241 with the result that the reads in the two processes
242 might (incorrectly) overlap in the blocks of data that they obtained.
243 This problem was fixed in Linux 3.14.
244 .\" http://thread.gmane.org/gmane.linux.kernel/1649458
245 .\" From: Michael Kerrisk (man-pages <mtk.manpages <at> gmail.com>
246 .\" Subject: Update of file offset on write() etc. is non-atomic with I/O
247 .\" Date: 2014-02-17 15:41:37 GMT
248 .\" Newsgroups: gmane.linux.kernel, gmane.linux.file-systems
249 .\" commit 9c225f2655e36a470c4f58dbbc99244c5fc7f2d4
250 .\" Author: Linus Torvalds <torvalds@linux-foundation.org>
251 .\" Date: Mon Mar 3 09:36:58 2014 -0800
252 .\"
253 .\" vfs: atomic f_pos accesses as per POSIX
254 .SH SEE ALSO
255 .BR close (2),
256 .BR fcntl (2),
257 .BR ioctl (2),
258 .BR lseek (2),
259 .BR open (2),
260 .BR pread (2),
261 .BR readdir (2),
262 .BR readlink (2),
263 .BR readv (2),
264 .BR select (2),
265 .BR write (2),
266 .BR fread (3)