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

AltStyle によって変換されたページ (->オリジナル) /