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: src/lib/libc/sys/read.2,v 1.9.2.6 2001/12/14 18:34:01 ru Exp $ 34.\" 35.Dd June 3, 2021 36.Dt READ 2 37.Os 38.Sh NAME 39.Nm pread , 40.Nm read , 41.Nm preadv , 42.Nm readv 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.Fo pread 52.Fa "int d" 53.Fa "void *buf" 54.Fa "size_t nbyte" 55.Fa "off_t offset" 56.Fc 57.Ft ssize_t 58.Fo read 59.Fa "int fildes" 60.Fa "void *buf" 61.Fa "size_t nbyte" 62.Fc 63.Ft ssize_t 64.Fo preadv 65.Fa "int d" 66.Fa "const struct iovec *iov" 67.Fa "int iovcnt" 68.Fa "off_t offset" 69.Fc 70.Ft ssize_t 71.Fo readv 72.Fa "int d" 73.Fa "const struct iovec *iov" 74.Fa "int iovcnt" 75.Fc 76.Sh DESCRIPTION 77.Fn read 78attempts to read 79.Fa nbyte 80bytes of data from the object referenced by the descriptor 81.Fa fildes 82into the buffer pointed to by 83.Fa buf . 84.Fn readv 85performs the same action, 86but scatters the input data into the 87.Fa iovcnt 88buffers specified by the members of the 89.Fa iov 90array: iov[0], iov[1], ..., iov[iovcnt\|\-\|1]. 91.Fn pread 92and 93.Fn preadv 94perform the same functions, 95but read from the specified position in the file 96without modifying the file pointer. 97.Pp 98For 99.Fn readv 100and 101.Fn preadv , 102the 103.Fa iovec 104structure is defined as: 105.Pp 106.Bd -literal -offset indent -compact 107struct iovec { 108 char *iov_base; /* Base address. */ 109 size_t iov_len; /* Length. */ 110}; 111.Ed 112.Pp 113Each 114.Fa iovec 115entry specifies the base address and length of an area 116in memory where data should be placed. 117.Fn readv 118and 119.Fn preadv 120will always fill an area completely before proceeding 121to the next. 122.Pp 123On objects capable of seeking, the 124.Fn read 125starts at a position 126given by the pointer associated with 127.Fa fildes 128(see 129.Xr lseek 2 ) . 130Upon return from 131.Fn read , 132the pointer is incremented by the number of bytes actually read. 133.Pp 134Objects that are not capable of seeking always read from the current 135position. The value of the pointer associated with such an 136object is undefined. 137.Pp 138Upon successful completion, 139.Fn read , 140.Fn readv , 141.Fn pread , 142and 143.Fn preadv 144return the number of bytes actually read and placed in the buffer. 145The system guarantees to read the number of bytes requested if 146the descriptor references a normal file that has that many bytes left 147before the end-of-file, but in no other case. 148.Pp 149.Fn read 150and 151.Fn pread 152will fail if the parameter 153.Fa nbyte 154exceeds 155.Dv INT_MAX , 156and they do not attempt a partial read. 157.Sh RETURN VALUES 158If successful, the 159number of bytes actually read is returned. 160Upon reading end-of-file, 161zero is returned. 162Otherwise, a -1 is returned and the global variable 163.Va errno 164is set to indicate the error. 165.Sh ERRORS 166The 167.Fn read , 168.Fn readv , 169.Fn pread , 170and 171.Fn preadv 172calls 173will succeed unless: 174.Bl -tag -width Er 175.\" =========== 176.It Bq Er EAGAIN 177The file was marked for non-blocking I/O, 178and no data were ready to be read. 179.\" =========== 180.It Bq Er EBADF 181.Fa fildes 182is not a valid file or socket descriptor open for reading. 183.\" =========== 184.It Bq Er EFAULT 185.Fa Buf 186points outside the allocated address space. 187.\" =========== 188.It Bq Er EINTR 189A read from a slow device was interrupted before 190any data arrived by the delivery of a signal. 191.It Bq Er EINVAL 192The pointer associated with 193.Fa fildes 194was negative. 195.\" =========== 196.It Bq Er EIO 197An I/O error occurred while reading from the file system. 198.\" =========== 199.\" .It Bq Er EIO 200.\" The process is a member of a background process 201.\" attempting to read from its controlling terminal. 202.\" =========== 203.\" .It Bq Er EIO 204.\" The process is ignoring or blocking the SIGTTIN signal. 205.\" =========== 206.It Bq Er EIO 207The process group is orphaned. 208.\" =========== 209.It Bq Er EIO 210The file is a regular file, 211.Fa nbyte 212is greater than 0, 213the starting position is before the end-of-file, 214and the starting position is greater than or equal 215to the offset maximum established 216for the open file descriptor associated with 217.Fa fildes . 218.\" =========== 219.It Bq Er EISDIR 220An attempt is made to read a directory. 221.\" =========== 222.It Bq Er ENOBUFS 223An attempt to allocate a memory buffer fails. 224.\" =========== 225.It Bq Er ENOMEM 226Insufficient memory is available. 227.\" =========== 228.It Bq Er ENXIO 229An action is requested of a device that does not exist. 230.\" =========== 231.It Bq Er ENXIO 232A requested action cannot be performed by the device. 233.\" =========== 234.It Bq Er ESTALE 235An attempt to read a remote file through NFS that has already been deleted in 236the server. 237.\" =========== 238.It Bq Er ETIMEDOUT 239The connection timed out while reading a remote file from a soft mounted NFS 240volume (see 241.Xr mount_nfs 8 ) . 242.\" =========== 243.It Bq Er ETIMEDOUT 244The file is a 245.Dq dataless 246file that requires materialization and materialization timed out or 247encountered some other temporary failure. 248.\" =========== 249.It Bq Er EDEADLK 250The file is a 251.Dq dataless 252file that requires materialization and the I/O policy of the current thread 253or process disallows dataless file materialization 254.Po see 255.Xr getiopolicy_np 3 256.Pc . 257.El 258.Pp 259.Fn pread 260and 261.Fn preadv 262calls may also return the following errors: 263.Bl -tag -width Er 264.\" =========== 265.It Bq Er EINVAL 266The specified file offset is invalid. 267.\" =========== 268.It Bq Er ESPIPE 269The file descriptor is associated with a pipe, socket, or FIFO. 270.El 271.Pp 272The 273.Fn read 274call may also return the following errors: 275.Bl -tag -width Er 276.\" =========== 277.It Bq Er ECONNRESET 278The connection is closed by the peer 279during a read attempt on a socket. 280.\" =========== 281.It Bq Er ENOTCONN 282A read is attempted on an unconnected socket. 283.\" =========== 284.It Bq Er ETIMEDOUT 285A transmission timeout occurs 286during a read attempt on a socket. 287.El 288.Pp 289The 290.Fn read 291and 292.Fn pread 293call may also return the following error: 294.Bl -tag -width Er 295.\" =========== 296.It Bq Er EINVAL 297The value provided for 298.Fa nbyte 299exceeds 300.Dv INT_MAX . 301.El 302.Pp 303The 304.Fn readv 305and 306.Fn preadv 307calls may also return one of the following errors: 308.Bl -tag -width Er 309.\" =========== 310.It Bq Er EFAULT 311Part of the 312.Fa iov 313points outside the process's allocated address space. 314.\" =========== 315.It Bq Er EINVAL 316.Fa Iovcnt 317was less than or equal to 0, or greater than 16. 318.\" =========== 319.It Bq Er EINVAL 320One of the 321.Fa iov_len 322values in the 323.Fa iov 324array was negative. 325.\" =========== 326.It Bq Er EINVAL 327The sum of the 328.Fa iov_len 329values in the 330.Fa iov 331array overflowed a 32-bit integer. 332.El 333.Sh LEGACY SYNOPSIS 334.Fd #include <sys/types.h> 335.Fd #include <sys/uio.h> 336.Fd #include <unistd.h> 337.Pp 338The include files 339.In sys/types.h 340and 341.In sys/uio.h 342are necessary for all functions. 343.Sh SEE ALSO 344.Xr dup 2 , 345.Xr fcntl 2 , 346.Xr open 2 , 347.Xr pipe 2 , 348.Xr select 2 , 349.Xr socket 2 , 350.Xr socketpair 2 , 351.Xr compat 5 352.Sh STANDARDS 353The 354.Fn read 355function call is expected to conform to 356.St -p1003.1-90 . 357The 358.Fn readv 359and 360.Fn pread 361functions are expected to conform to 362.St -xpg4.2 . 363.Fn preadv 364is nonstandard. 365.Sh HISTORY 366The 367.Fn pread 368function call 369appeared in 370.At V.4 . 371The 372.Fn readv 373function call 374appeared in 375.Bx 4.2 . 376A 377.Fn read 378function call appeared in 379.At v6 . 380