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