]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - lib/libc/sys/read.2
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.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 October 11, 2006
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 sys/types.h
44 .In sys/uio.h
45 .In unistd.h
46 .Ft ssize_t
47 .Fn read "int d" "void *buf" "size_t nbytes"
48 .Ft ssize_t
49 .Fn pread "int d" "void *buf" "size_t nbytes" "off_t offset"
50 .Ft ssize_t
51 .Fn readv "int d" "const struct iovec *iov" "int iovcnt"
52 .Ft ssize_t
53 .Fn preadv "int d" "const struct iovec *iov" "int iovcnt" "off_t offset"
54 .Sh DESCRIPTION
55 The
56 .Fn read
57 system call
58 attempts to read
59 .Fa nbytes
60 of data from the object referenced by the descriptor
61 .Fa d
62 into the buffer pointed to by
63 .Fa buf .
64 The
65 .Fn readv
66 system call
67 performs the same action, but scatters the input data
68 into the
69 .Fa iovcnt
70 buffers specified by the members of the
71 .Fa iov
72 array: iov[0], iov[1], ..., iov[iovcnt\|\-\|1].
73 The
74 .Fn pread
75 and
76 .Fn preadv
77 system calls
78 perform the same functions, but read from the specified position in
79 the file without modifying the file pointer.
80 .Pp
81 For
82 .Fn readv
83 and
84 .Fn preadv ,
85 the
86 .Fa iovec
87 structure is defined as:
88 .Pp
89 .Bd -literal -offset indent -compact
90 struct iovec {
91         void   *iov_base;  /* Base address. */
92         size_t iov_len;    /* Length. */
93 };
94 .Ed
95 .Pp
96 Each
97 .Fa iovec
98 entry specifies the base address and length of an area
99 in memory where data should be placed.
100 The
101 .Fn readv
102 system call
103 will always fill an area completely before proceeding
104 to the next.
105 .Pp
106 On objects capable of seeking, the
107 .Fn read
108 starts at a position
109 given by the pointer associated with
110 .Fa d
111 (see
112 .Xr lseek 2 ) .
113 Upon return from
114 .Fn read ,
115 the pointer is incremented by the number of bytes actually read.
116 .Pp
117 Objects that are not capable of seeking always read from the current
118 position.
119 The value of the pointer associated with such an
120 object is undefined.
121 .Pp
122 Upon successful completion,
123 .Fn read ,
124 .Fn readv ,
125 .Fn pread
126 and
127 .Fn preadv
128 return the number of bytes actually read and placed in the buffer.
129 The system guarantees to read the number of bytes requested if
130 the descriptor references a normal file that has that many bytes left
131 before the end-of-file, but in no other case.
132 .Sh RETURN VALUES
133 If successful, the
134 number of bytes actually read is returned.
135 Upon reading end-of-file,
136 zero is returned.
137 Otherwise, a -1 is returned and the global variable
138 .Va errno
139 is set to indicate the error.
140 .Sh ERRORS
141 The
142 .Fn read ,
143 .Fn readv ,
144 .Fn pread
145 and
146 .Fn preadv
147 system calls
148 will succeed unless:
149 .Bl -tag -width Er
150 .It Bq Er EBADF
151 The
152 .Fa d
153 argument
154 is not a valid file or socket descriptor open for reading.
155 .It Bq Er ECONNRESET
156 The
157 .Fa d
158 argument refers to a socket, and the remote socket end is
159 forcibly closed.
160 .It Bq Er EFAULT
161 The
162 .Fa buf
163 argument
164 points outside the allocated address space.
165 .It Bq Er EIO
166 An I/O error occurred while reading from the file system.
167 .It Bq Er EBUSY
168 Failed to read from a file, e.g. /proc/<pid>/regs while <pid> is not stopped
169 .It Bq Er EINTR
170 A read from a slow device
171 (i.e.\& one that might block for an arbitrary amount of time)
172 was interrupted by the delivery of a signal
173 before any data arrived.
174 .It Bq Er EINVAL
175 The pointer associated with
176 .Fa d
177 was negative.
178 .It Bq Er EAGAIN
179 The file was marked for non-blocking I/O,
180 and no data were ready to be read.
181 .It Bq Er EISDIR
182 The file descriptor is associated with a directory residing
183 on a file system that does not allow regular read operations on
184 directories (e.g.\& NFS).
185 .It Bq Er EOPNOTSUPP
186 The file descriptor is associated with a file system and file type that
187 do not allow regular read operations on it.
188 .It Bq Er EOVERFLOW
189 The file descriptor is associated with a regular file,
190 .Fa nbytes
191 is greater than 0,
192 .Fa offset
193 is before the end-of-file, and
194 .Fa offset
195 is greater than or equal to the offset maximum established
196 for this file system.
197 .It Bq Er EINVAL
198 The value
199 .Fa nbytes
200 is greater than
201 .Dv INT_MAX .
202 .El
203 .Pp
204 In addition,
205 .Fn readv
206 and
207 .Fn preadv
208 may return one of the following errors:
209 .Bl -tag -width Er
210 .It Bq Er EINVAL
211 The
212 .Fa iovcnt
213 argument
214 was less than or equal to 0, or greater than
215 .Dv IOV_MAX .
216 .It Bq Er EINVAL
217 One of the
218 .Fa iov_len
219 values in the
220 .Fa iov
221 array was negative.
222 .It Bq Er EINVAL
223 The sum of the
224 .Fa iov_len
225 values in the
226 .Fa iov
227 array overflowed a 32-bit integer.
228 .It Bq Er EFAULT
229 Part of the
230 .Fa iov
231 array points outside the process's allocated address space.
232 .El
233 .Pp
234 The
235 .Fn pread
236 and
237 .Fn preadv
238 system calls may also return the following errors:
239 .Bl -tag -width Er
240 .It Bq Er EINVAL
241 The
242 .Fa offset
243 value was negative.
244 .It Bq Er ESPIPE
245 The file descriptor is associated with a pipe, socket, or FIFO.
246 .El
247 .Sh SEE ALSO
248 .Xr dup 2 ,
249 .Xr fcntl 2 ,
250 .Xr getdirentries 2 ,
251 .Xr open 2 ,
252 .Xr pipe 2 ,
253 .Xr select 2 ,
254 .Xr socket 2 ,
255 .Xr socketpair 2 ,
256 .Xr fread 3 ,
257 .Xr readdir 3
258 .Sh STANDARDS
259 The
260 .Fn read
261 system call is expected to conform to
262 .St -p1003.1-90 .
263 The
264 .Fn readv
265 and
266 .Fn pread
267 system calls are expected to conform to
268 .St -xpg4.2 .
269 .Sh HISTORY
270 The
271 .Fn preadv
272 system call appeared in
273 .Fx 6.0 .
274 The
275 .Fn pread
276 function appeared in
277 .At V.4 .
278 The
279 .Fn readv
280 system call appeared in
281 .Bx 4.2 .
282 The
283 .Fn read
284 function appeared in
285 .At v6 .