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 June 4, 2020 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.Xr read 2 133and 134.Xr write 2 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.Xr read 2 139or 140.Xr write 2 , 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 INT_MAX . 224.El 225.Pp 226In addition, 227.Fn readv 228and 229.Fn preadv 230may return one of the following errors: 231.Bl -tag -width Er 232.It Bq Er EINVAL 233The 234.Fa iovcnt 235argument 236was less than or equal to 0, or greater than 237.Dv IOV_MAX . 238.It Bq Er EINVAL 239One of the 240.Fa iov_len 241values in the 242.Fa iov 243array was negative. 244.It Bq Er EINVAL 245The sum of the 246.Fa iov_len 247values in the 248.Fa iov 249array overflowed a 32-bit integer. 250.It Bq Er EFAULT 251Part of the 252.Fa iov 253array points outside the process's allocated address space. 254.El 255.Pp 256The 257.Fn pread 258and 259.Fn preadv 260system calls may also return the following errors: 261.Bl -tag -width Er 262.It Bq Er EINVAL 263The 264.Fa offset 265value was negative. 266.It Bq Er ESPIPE 267The file descriptor is associated with a pipe, socket, or FIFO. 268.El 269.Sh SEE ALSO 270.Xr dup 2 , 271.Xr fcntl 2 , 272.Xr getdirentries 2 , 273.Xr open 2 , 274.Xr pipe 2 , 275.Xr select 2 , 276.Xr socket 2 , 277.Xr socketpair 2 , 278.Xr fread 3 , 279.Xr readdir 3 280.Sh STANDARDS 281The 282.Fn read 283system call is expected to conform to 284.St -p1003.1-90 . 285The 286.Fn readv 287and 288.Fn pread 289system calls are expected to conform to 290.St -xpg4.2 . 291.Sh HISTORY 292The 293.Fn preadv 294system call appeared in 295.Fx 6.0 . 296The 297.Fn pread 298function appeared in 299.At V.4 . 300The 301.Fn readv 302system call appeared in 303.Bx 4.2 . 304The 305.Fn read 306function appeared in 307.At v1 . 308