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