xref: /freebsd/lib/libsys/read.2 (revision 8269e7673cf033aba67dab8264fe719920c70f87)
1*8269e767SBrooks Davis.\" Copyright (c) 1980, 1991, 1993
2*8269e767SBrooks Davis.\"	The Regents of the University of California.  All rights reserved.
3*8269e767SBrooks Davis.\"
4*8269e767SBrooks Davis.\" Redistribution and use in source and binary forms, with or without
5*8269e767SBrooks Davis.\" modification, are permitted provided that the following conditions
6*8269e767SBrooks Davis.\" are met:
7*8269e767SBrooks Davis.\" 1. Redistributions of source code must retain the above copyright
8*8269e767SBrooks Davis.\"    notice, this list of conditions and the following disclaimer.
9*8269e767SBrooks Davis.\" 2. Redistributions in binary form must reproduce the above copyright
10*8269e767SBrooks Davis.\"    notice, this list of conditions and the following disclaimer in the
11*8269e767SBrooks Davis.\"    documentation and/or other materials provided with the distribution.
12*8269e767SBrooks Davis.\" 3. Neither the name of the University nor the names of its contributors
13*8269e767SBrooks Davis.\"    may be used to endorse or promote products derived from this software
14*8269e767SBrooks Davis.\"    without specific prior written permission.
15*8269e767SBrooks Davis.\"
16*8269e767SBrooks Davis.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17*8269e767SBrooks Davis.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*8269e767SBrooks Davis.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*8269e767SBrooks Davis.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20*8269e767SBrooks Davis.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*8269e767SBrooks Davis.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*8269e767SBrooks Davis.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*8269e767SBrooks Davis.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*8269e767SBrooks Davis.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*8269e767SBrooks Davis.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8269e767SBrooks Davis.\" SUCH DAMAGE.
27*8269e767SBrooks Davis.\"
28*8269e767SBrooks Davis.Dd June 4, 2020
29*8269e767SBrooks Davis.Dt READ 2
30*8269e767SBrooks Davis.Os
31*8269e767SBrooks Davis.Sh NAME
32*8269e767SBrooks Davis.Nm read ,
33*8269e767SBrooks Davis.Nm readv ,
34*8269e767SBrooks Davis.Nm pread ,
35*8269e767SBrooks Davis.Nm preadv
36*8269e767SBrooks Davis.Nd read input
37*8269e767SBrooks Davis.Sh LIBRARY
38*8269e767SBrooks Davis.Lb libc
39*8269e767SBrooks Davis.Sh SYNOPSIS
40*8269e767SBrooks Davis.In unistd.h
41*8269e767SBrooks Davis.Ft ssize_t
42*8269e767SBrooks Davis.Fn read "int fd" "void *buf" "size_t nbytes"
43*8269e767SBrooks Davis.Ft ssize_t
44*8269e767SBrooks Davis.Fn pread "int fd" "void *buf" "size_t nbytes" "off_t offset"
45*8269e767SBrooks Davis.In sys/uio.h
46*8269e767SBrooks Davis.Ft ssize_t
47*8269e767SBrooks Davis.Fn readv "int fd" "const struct iovec *iov" "int iovcnt"
48*8269e767SBrooks Davis.Ft ssize_t
49*8269e767SBrooks Davis.Fn preadv "int fd" "const struct iovec *iov" "int iovcnt" "off_t offset"
50*8269e767SBrooks Davis.Sh DESCRIPTION
51*8269e767SBrooks DavisThe
52*8269e767SBrooks Davis.Fn read
53*8269e767SBrooks Davissystem call
54*8269e767SBrooks Davisattempts to read
55*8269e767SBrooks Davis.Fa nbytes
56*8269e767SBrooks Davisof data from the object referenced by the descriptor
57*8269e767SBrooks Davis.Fa fd
58*8269e767SBrooks Davisinto the buffer pointed to by
59*8269e767SBrooks Davis.Fa buf .
60*8269e767SBrooks DavisThe
61*8269e767SBrooks Davis.Fn readv
62*8269e767SBrooks Davissystem call
63*8269e767SBrooks Davisperforms the same action, but scatters the input data
64*8269e767SBrooks Davisinto the
65*8269e767SBrooks Davis.Fa iovcnt
66*8269e767SBrooks Davisbuffers specified by the members of the
67*8269e767SBrooks Davis.Fa iov
68*8269e767SBrooks Davisarray: iov[0], iov[1], ..., iov[iovcnt\|\-\|1].
69*8269e767SBrooks DavisThe
70*8269e767SBrooks Davis.Fn pread
71*8269e767SBrooks Davisand
72*8269e767SBrooks Davis.Fn preadv
73*8269e767SBrooks Davissystem calls
74*8269e767SBrooks Davisperform the same functions, but read from the specified position in
75*8269e767SBrooks Davisthe file without modifying the file pointer.
76*8269e767SBrooks Davis.Pp
77*8269e767SBrooks DavisFor
78*8269e767SBrooks Davis.Fn readv
79*8269e767SBrooks Davisand
80*8269e767SBrooks Davis.Fn preadv ,
81*8269e767SBrooks Davisthe
82*8269e767SBrooks Davis.Fa iovec
83*8269e767SBrooks Davisstructure is defined as:
84*8269e767SBrooks Davis.Pp
85*8269e767SBrooks Davis.Bd -literal -offset indent -compact
86*8269e767SBrooks Davisstruct iovec {
87*8269e767SBrooks Davis	void   *iov_base;  /* Base address. */
88*8269e767SBrooks Davis	size_t iov_len;    /* Length. */
89*8269e767SBrooks Davis};
90*8269e767SBrooks Davis.Ed
91*8269e767SBrooks Davis.Pp
92*8269e767SBrooks DavisEach
93*8269e767SBrooks Davis.Fa iovec
94*8269e767SBrooks Davisentry specifies the base address and length of an area
95*8269e767SBrooks Davisin memory where data should be placed.
96*8269e767SBrooks DavisThe
97*8269e767SBrooks Davis.Fn readv
98*8269e767SBrooks Davissystem call
99*8269e767SBrooks Daviswill always fill an area completely before proceeding
100*8269e767SBrooks Davisto the next.
101*8269e767SBrooks Davis.Pp
102*8269e767SBrooks DavisOn objects capable of seeking, the
103*8269e767SBrooks Davis.Fn read
104*8269e767SBrooks Davisstarts at a position
105*8269e767SBrooks Davisgiven by the pointer associated with
106*8269e767SBrooks Davis.Fa fd
107*8269e767SBrooks Davis(see
108*8269e767SBrooks Davis.Xr lseek 2 ) .
109*8269e767SBrooks DavisUpon return from
110*8269e767SBrooks Davis.Fn read ,
111*8269e767SBrooks Davisthe pointer is incremented by the number of bytes actually read.
112*8269e767SBrooks Davis.Pp
113*8269e767SBrooks DavisObjects that are not capable of seeking always read from the current
114*8269e767SBrooks Davisposition.
115*8269e767SBrooks DavisThe value of the pointer associated with such an
116*8269e767SBrooks Davisobject is undefined.
117*8269e767SBrooks Davis.Pp
118*8269e767SBrooks DavisUpon successful completion,
119*8269e767SBrooks Davis.Fn read ,
120*8269e767SBrooks Davis.Fn readv ,
121*8269e767SBrooks Davis.Fn pread
122*8269e767SBrooks Davisand
123*8269e767SBrooks Davis.Fn preadv
124*8269e767SBrooks Davisreturn the number of bytes actually read and placed in the buffer.
125*8269e767SBrooks DavisThe system guarantees to read the number of bytes requested if
126*8269e767SBrooks Davisthe descriptor references a normal file that has that many bytes left
127*8269e767SBrooks Davisbefore the end-of-file, but in no other case.
128*8269e767SBrooks Davis.Pp
129*8269e767SBrooks DavisIn accordance with
130*8269e767SBrooks Davis.St -p1003.1-2004 ,
131*8269e767SBrooks Davisboth
132*8269e767SBrooks Davis.Xr read 2
133*8269e767SBrooks Davisand
134*8269e767SBrooks Davis.Xr write 2
135*8269e767SBrooks Davissyscalls are atomic with respect to each other in the effects on file
136*8269e767SBrooks Daviscontent, when they operate on regular files.
137*8269e767SBrooks DavisIf two threads each call one of the
138*8269e767SBrooks Davis.Xr read 2
139*8269e767SBrooks Davisor
140*8269e767SBrooks Davis.Xr write 2 ,
141*8269e767SBrooks Davissyscalls, each call will see either all of the changes of the other call,
142*8269e767SBrooks Davisor none of them.
143*8269e767SBrooks DavisThe
144*8269e767SBrooks Davis.Fx
145*8269e767SBrooks Daviskernel implements this guarantee by locking the file ranges affected by
146*8269e767SBrooks Davisthe calls.
147*8269e767SBrooks Davis.Sh RETURN VALUES
148*8269e767SBrooks DavisIf successful, the
149*8269e767SBrooks Davisnumber of bytes actually read is returned.
150*8269e767SBrooks DavisUpon reading end-of-file,
151*8269e767SBrooks Daviszero is returned.
152*8269e767SBrooks DavisOtherwise, a -1 is returned and the global variable
153*8269e767SBrooks Davis.Va errno
154*8269e767SBrooks Davisis set to indicate the error.
155*8269e767SBrooks Davis.Sh ERRORS
156*8269e767SBrooks DavisThe
157*8269e767SBrooks Davis.Fn read ,
158*8269e767SBrooks Davis.Fn readv ,
159*8269e767SBrooks Davis.Fn pread
160*8269e767SBrooks Davisand
161*8269e767SBrooks Davis.Fn preadv
162*8269e767SBrooks Davissystem calls
163*8269e767SBrooks Daviswill succeed unless:
164*8269e767SBrooks Davis.Bl -tag -width Er
165*8269e767SBrooks Davis.It Bq Er EBADF
166*8269e767SBrooks DavisThe
167*8269e767SBrooks Davis.Fa fd
168*8269e767SBrooks Davisargument
169*8269e767SBrooks Davisis not a valid file or socket descriptor open for reading.
170*8269e767SBrooks Davis.It Bq Er ECONNRESET
171*8269e767SBrooks DavisThe
172*8269e767SBrooks Davis.Fa fd
173*8269e767SBrooks Davisargument refers to a socket, and the remote socket end is
174*8269e767SBrooks Davisforcibly closed.
175*8269e767SBrooks Davis.It Bq Er EFAULT
176*8269e767SBrooks DavisThe
177*8269e767SBrooks Davis.Fa buf
178*8269e767SBrooks Davisargument
179*8269e767SBrooks Davispoints outside the allocated address space.
180*8269e767SBrooks Davis.It Bq Er EIO
181*8269e767SBrooks DavisAn I/O error occurred while reading from the file system.
182*8269e767SBrooks Davis.It Bq Er EINTEGRITY
183*8269e767SBrooks DavisCorrupted data was detected while reading from the file system.
184*8269e767SBrooks Davis.It Bq Er EBUSY
185*8269e767SBrooks DavisFailed to read from a file, e.g. /proc/<pid>/regs while <pid> is not stopped
186*8269e767SBrooks Davis.It Bq Er EINTR
187*8269e767SBrooks DavisA read from a slow device
188*8269e767SBrooks Davis(i.e.\& one that might block for an arbitrary amount of time)
189*8269e767SBrooks Daviswas interrupted by the delivery of a signal
190*8269e767SBrooks Davisbefore any data arrived.
191*8269e767SBrooks Davis.It Bq Er EINVAL
192*8269e767SBrooks DavisThe pointer associated with
193*8269e767SBrooks Davis.Fa fd
194*8269e767SBrooks Daviswas negative.
195*8269e767SBrooks Davis.It Bq Er EAGAIN
196*8269e767SBrooks DavisThe file was marked for non-blocking I/O,
197*8269e767SBrooks Davisand no data were ready to be read.
198*8269e767SBrooks Davis.It Bq Er EISDIR
199*8269e767SBrooks DavisThe file descriptor is associated with a directory.
200*8269e767SBrooks DavisDirectories may only be read directly by root if the filesystem supports it and
201*8269e767SBrooks Davisthe
202*8269e767SBrooks Davis.Dv security.bsd.allow_read_dir
203*8269e767SBrooks Davissysctl MIB is set to a non-zero value.
204*8269e767SBrooks DavisFor most scenarios, the
205*8269e767SBrooks Davis.Xr readdir 3
206*8269e767SBrooks Davisfunction should be used instead.
207*8269e767SBrooks Davis.It Bq Er EOPNOTSUPP
208*8269e767SBrooks DavisThe file descriptor is associated with a file system and file type that
209*8269e767SBrooks Davisdo not allow regular read operations on it.
210*8269e767SBrooks Davis.It Bq Er EOVERFLOW
211*8269e767SBrooks DavisThe file descriptor is associated with a regular file,
212*8269e767SBrooks Davis.Fa nbytes
213*8269e767SBrooks Davisis greater than 0,
214*8269e767SBrooks Davis.Fa offset
215*8269e767SBrooks Davisis before the end-of-file, and
216*8269e767SBrooks Davis.Fa offset
217*8269e767SBrooks Davisis greater than or equal to the offset maximum established
218*8269e767SBrooks Davisfor this file system.
219*8269e767SBrooks Davis.It Bq Er EINVAL
220*8269e767SBrooks DavisThe value
221*8269e767SBrooks Davis.Fa nbytes
222*8269e767SBrooks Davisis greater than
223*8269e767SBrooks Davis.Dv INT_MAX .
224*8269e767SBrooks Davis.El
225*8269e767SBrooks Davis.Pp
226*8269e767SBrooks DavisIn addition,
227*8269e767SBrooks Davis.Fn readv
228*8269e767SBrooks Davisand
229*8269e767SBrooks Davis.Fn preadv
230*8269e767SBrooks Davismay return one of the following errors:
231*8269e767SBrooks Davis.Bl -tag -width Er
232*8269e767SBrooks Davis.It Bq Er EINVAL
233*8269e767SBrooks DavisThe
234*8269e767SBrooks Davis.Fa iovcnt
235*8269e767SBrooks Davisargument
236*8269e767SBrooks Daviswas less than or equal to 0, or greater than
237*8269e767SBrooks Davis.Dv IOV_MAX .
238*8269e767SBrooks Davis.It Bq Er EINVAL
239*8269e767SBrooks DavisOne of the
240*8269e767SBrooks Davis.Fa iov_len
241*8269e767SBrooks Davisvalues in the
242*8269e767SBrooks Davis.Fa iov
243*8269e767SBrooks Davisarray was negative.
244*8269e767SBrooks Davis.It Bq Er EINVAL
245*8269e767SBrooks DavisThe sum of the
246*8269e767SBrooks Davis.Fa iov_len
247*8269e767SBrooks Davisvalues in the
248*8269e767SBrooks Davis.Fa iov
249*8269e767SBrooks Davisarray overflowed a 32-bit integer.
250*8269e767SBrooks Davis.It Bq Er EFAULT
251*8269e767SBrooks DavisPart of the
252*8269e767SBrooks Davis.Fa iov
253*8269e767SBrooks Davisarray points outside the process's allocated address space.
254*8269e767SBrooks Davis.El
255*8269e767SBrooks Davis.Pp
256*8269e767SBrooks DavisThe
257*8269e767SBrooks Davis.Fn pread
258*8269e767SBrooks Davisand
259*8269e767SBrooks Davis.Fn preadv
260*8269e767SBrooks Davissystem calls may also return the following errors:
261*8269e767SBrooks Davis.Bl -tag -width Er
262*8269e767SBrooks Davis.It Bq Er EINVAL
263*8269e767SBrooks DavisThe
264*8269e767SBrooks Davis.Fa offset
265*8269e767SBrooks Davisvalue was negative.
266*8269e767SBrooks Davis.It Bq Er ESPIPE
267*8269e767SBrooks DavisThe file descriptor is associated with a pipe, socket, or FIFO.
268*8269e767SBrooks Davis.El
269*8269e767SBrooks Davis.Sh SEE ALSO
270*8269e767SBrooks Davis.Xr dup 2 ,
271*8269e767SBrooks Davis.Xr fcntl 2 ,
272*8269e767SBrooks Davis.Xr getdirentries 2 ,
273*8269e767SBrooks Davis.Xr open 2 ,
274*8269e767SBrooks Davis.Xr pipe 2 ,
275*8269e767SBrooks Davis.Xr select 2 ,
276*8269e767SBrooks Davis.Xr socket 2 ,
277*8269e767SBrooks Davis.Xr socketpair 2 ,
278*8269e767SBrooks Davis.Xr fread 3 ,
279*8269e767SBrooks Davis.Xr readdir 3
280*8269e767SBrooks Davis.Sh STANDARDS
281*8269e767SBrooks DavisThe
282*8269e767SBrooks Davis.Fn read
283*8269e767SBrooks Davissystem call is expected to conform to
284*8269e767SBrooks Davis.St -p1003.1-90 .
285*8269e767SBrooks DavisThe
286*8269e767SBrooks Davis.Fn readv
287*8269e767SBrooks Davisand
288*8269e767SBrooks Davis.Fn pread
289*8269e767SBrooks Davissystem calls are expected to conform to
290*8269e767SBrooks Davis.St -xpg4.2 .
291*8269e767SBrooks Davis.Sh HISTORY
292*8269e767SBrooks DavisThe
293*8269e767SBrooks Davis.Fn preadv
294*8269e767SBrooks Davissystem call appeared in
295*8269e767SBrooks Davis.Fx 6.0 .
296*8269e767SBrooks DavisThe
297*8269e767SBrooks Davis.Fn pread
298*8269e767SBrooks Davisfunction appeared in
299*8269e767SBrooks Davis.At V.4 .
300*8269e767SBrooks DavisThe
301*8269e767SBrooks Davis.Fn readv
302*8269e767SBrooks Davissystem call appeared in
303*8269e767SBrooks Davis.Bx 4.2 .
304*8269e767SBrooks DavisThe
305*8269e767SBrooks Davis.Fn read
306*8269e767SBrooks Davisfunction appeared in
307*8269e767SBrooks Davis.At v1 .
308