]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - lib/libc/sys/read.2
MFC r292268, r334176
[FreeBSD/stable/10.git] / lib / libc / sys / read.2
1 .\" Copyright (c) 1980, 1991, 1993
2 .\"     The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\" 4. Neither the name of the University nor the names of its contributors
13 .\"    may be used to endorse or promote products derived from this software
14 .\"    without specific prior written permission.
15 .\"
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 .\" SUCH DAMAGE.
27 .\"
28 .\"     @(#)read.2      8.4 (Berkeley) 2/26/94
29 .\" $FreeBSD$
30 .\"
31 .Dd December 15, 2015
32 .Dt READ 2
33 .Os
34 .Sh NAME
35 .Nm read ,
36 .Nm readv ,
37 .Nm pread ,
38 .Nm preadv
39 .Nd read input
40 .Sh LIBRARY
41 .Lb libc
42 .Sh SYNOPSIS
43 .In unistd.h
44 .Ft ssize_t
45 .Fn read "int fd" "void *buf" "size_t nbytes"
46 .Ft ssize_t
47 .Fn pread "int fd" "void *buf" "size_t nbytes" "off_t offset"
48 .In sys/uio.h
49 .Ft ssize_t
50 .Fn readv "int fd" "const struct iovec *iov" "int iovcnt"
51 .Ft ssize_t
52 .Fn preadv "int fd" "const struct iovec *iov" "int iovcnt" "off_t offset"
53 .Sh DESCRIPTION
54 The
55 .Fn read
56 system call
57 attempts to read
58 .Fa nbytes
59 of data from the object referenced by the descriptor
60 .Fa fd
61 into the buffer pointed to by
62 .Fa buf .
63 The
64 .Fn readv
65 system call
66 performs the same action, but scatters the input data
67 into the
68 .Fa iovcnt
69 buffers specified by the members of the
70 .Fa iov
71 array: iov[0], iov[1], ..., iov[iovcnt\|\-\|1].
72 The
73 .Fn pread
74 and
75 .Fn preadv
76 system calls
77 perform the same functions, but read from the specified position in
78 the file without modifying the file pointer.
79 .Pp
80 For
81 .Fn readv
82 and
83 .Fn preadv ,
84 the
85 .Fa iovec
86 structure is defined as:
87 .Pp
88 .Bd -literal -offset indent -compact
89 struct iovec {
90         void   *iov_base;  /* Base address. */
91         size_t iov_len;    /* Length. */
92 };
93 .Ed
94 .Pp
95 Each
96 .Fa iovec
97 entry specifies the base address and length of an area
98 in memory where data should be placed.
99 The
100 .Fn readv
101 system call
102 will always fill an area completely before proceeding
103 to the next.
104 .Pp
105 On objects capable of seeking, the
106 .Fn read
107 starts at a position
108 given by the pointer associated with
109 .Fa fd
110 (see
111 .Xr lseek 2 ) .
112 Upon return from
113 .Fn read ,
114 the pointer is incremented by the number of bytes actually read.
115 .Pp
116 Objects that are not capable of seeking always read from the current
117 position.
118 The value of the pointer associated with such an
119 object is undefined.
120 .Pp
121 Upon successful completion,
122 .Fn read ,
123 .Fn readv ,
124 .Fn pread
125 and
126 .Fn preadv
127 return the number of bytes actually read and placed in the buffer.
128 The system guarantees to read the number of bytes requested if
129 the descriptor references a normal file that has that many bytes left
130 before the end-of-file, but in no other case.
131 .Sh RETURN VALUES
132 If successful, the
133 number of bytes actually read is returned.
134 Upon reading end-of-file,
135 zero is returned.
136 Otherwise, a -1 is returned and the global variable
137 .Va errno
138 is set to indicate the error.
139 .Sh ERRORS
140 The
141 .Fn read ,
142 .Fn readv ,
143 .Fn pread
144 and
145 .Fn preadv
146 system calls
147 will succeed unless:
148 .Bl -tag -width Er
149 .It Bq Er EBADF
150 The
151 .Fa fd
152 argument
153 is not a valid file or socket descriptor open for reading.
154 .It Bq Er ECONNRESET
155 The
156 .Fa fd
157 argument refers to a socket, and the remote socket end is
158 forcibly closed.
159 .It Bq Er EFAULT
160 The
161 .Fa buf
162 argument
163 points outside the allocated address space.
164 .It Bq Er EIO
165 An I/O error occurred while reading from the file system.
166 .It Bq Er EBUSY
167 Failed to read from a file, e.g. /proc/<pid>/regs while <pid> is not stopped
168 .It Bq Er EINTR
169 A read from a slow device
170 (i.e.\& one that might block for an arbitrary amount of time)
171 was interrupted by the delivery of a signal
172 before any data arrived.
173 .It Bq Er EINVAL
174 The pointer associated with
175 .Fa fd
176 was negative.
177 .It Bq Er EAGAIN
178 The file was marked for non-blocking I/O,
179 and no data were ready to be read.
180 .It Bq Er EISDIR
181 The file descriptor is associated with a directory residing
182 on a file system that does not allow regular read operations on
183 directories (e.g.\& NFS).
184 .It Bq Er EOPNOTSUPP
185 The file descriptor is associated with a file system and file type that
186 do not allow regular read operations on it.
187 .It Bq Er EOVERFLOW
188 The file descriptor is associated with a regular file,
189 .Fa nbytes
190 is greater than 0,
191 .Fa offset
192 is before the end-of-file, and
193 .Fa offset
194 is greater than or equal to the offset maximum established
195 for this file system.
196 .It Bq Er EINVAL
197 The value
198 .Fa nbytes
199 is greater than
200 .Dv INT_MAX .
201 .El
202 .Pp
203 In addition,
204 .Fn readv
205 and
206 .Fn preadv
207 may return one of the following errors:
208 .Bl -tag -width Er
209 .It Bq Er EINVAL
210 The
211 .Fa iovcnt
212 argument
213 was less than or equal to 0, or greater than
214 .Dv IOV_MAX .
215 .It Bq Er EINVAL
216 One of the
217 .Fa iov_len
218 values in the
219 .Fa iov
220 array was negative.
221 .It Bq Er EINVAL
222 The sum of the
223 .Fa iov_len
224 values in the
225 .Fa iov
226 array overflowed a 32-bit integer.
227 .It Bq Er EFAULT
228 Part of the
229 .Fa iov
230 array points outside the process's allocated address space.
231 .El
232 .Pp
233 The
234 .Fn pread
235 and
236 .Fn preadv
237 system calls may also return the following errors:
238 .Bl -tag -width Er
239 .It Bq Er EINVAL
240 The
241 .Fa offset
242 value was negative.
243 .It Bq Er ESPIPE
244 The file descriptor is associated with a pipe, socket, or FIFO.
245 .El
246 .Sh SEE ALSO
247 .Xr dup 2 ,
248 .Xr fcntl 2 ,
249 .Xr getdirentries 2 ,
250 .Xr open 2 ,
251 .Xr pipe 2 ,
252 .Xr select 2 ,
253 .Xr socket 2 ,
254 .Xr socketpair 2 ,
255 .Xr fread 3 ,
256 .Xr readdir 3
257 .Sh STANDARDS
258 The
259 .Fn read
260 system call is expected to conform to
261 .St -p1003.1-90 .
262 The
263 .Fn readv
264 and
265 .Fn pread
266 system calls are expected to conform to
267 .St -xpg4.2 .
268 .Sh HISTORY
269 The
270 .Fn preadv
271 system call appeared in
272 .Fx 6.0 .
273 The
274 .Fn pread
275 function appeared in
276 .At V.4 .
277 The
278 .Fn readv
279 system call appeared in
280 .Bx 4.2 .
281 The
282 .Fn read
283 function appeared in
284 .At v6 .