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