18269e767SBrooks Davis.\" Copyright (c) 1980, 1991, 1993 28269e767SBrooks Davis.\" The Regents of the University of California. All rights reserved. 38269e767SBrooks Davis.\" 48269e767SBrooks Davis.\" Redistribution and use in source and binary forms, with or without 58269e767SBrooks Davis.\" modification, are permitted provided that the following conditions 68269e767SBrooks Davis.\" are met: 78269e767SBrooks Davis.\" 1. Redistributions of source code must retain the above copyright 88269e767SBrooks Davis.\" notice, this list of conditions and the following disclaimer. 98269e767SBrooks Davis.\" 2. Redistributions in binary form must reproduce the above copyright 108269e767SBrooks Davis.\" notice, this list of conditions and the following disclaimer in the 118269e767SBrooks Davis.\" documentation and/or other materials provided with the distribution. 128269e767SBrooks Davis.\" 3. Neither the name of the University nor the names of its contributors 138269e767SBrooks Davis.\" may be used to endorse or promote products derived from this software 148269e767SBrooks Davis.\" without specific prior written permission. 158269e767SBrooks Davis.\" 168269e767SBrooks Davis.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 178269e767SBrooks Davis.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 188269e767SBrooks Davis.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 198269e767SBrooks Davis.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 208269e767SBrooks Davis.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 218269e767SBrooks Davis.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 228269e767SBrooks Davis.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 238269e767SBrooks Davis.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 248269e767SBrooks Davis.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 258269e767SBrooks Davis.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 268269e767SBrooks Davis.\" SUCH DAMAGE. 278269e767SBrooks Davis.\" 28*beadbca6SWarner Losh.Dd March 1, 2024 298269e767SBrooks Davis.Dt READ 2 308269e767SBrooks Davis.Os 318269e767SBrooks Davis.Sh NAME 328269e767SBrooks Davis.Nm read , 338269e767SBrooks Davis.Nm readv , 348269e767SBrooks Davis.Nm pread , 358269e767SBrooks Davis.Nm preadv 368269e767SBrooks Davis.Nd read input 378269e767SBrooks Davis.Sh LIBRARY 388269e767SBrooks Davis.Lb libc 398269e767SBrooks Davis.Sh SYNOPSIS 408269e767SBrooks Davis.In unistd.h 418269e767SBrooks Davis.Ft ssize_t 428269e767SBrooks Davis.Fn read "int fd" "void *buf" "size_t nbytes" 438269e767SBrooks Davis.Ft ssize_t 448269e767SBrooks Davis.Fn pread "int fd" "void *buf" "size_t nbytes" "off_t offset" 458269e767SBrooks Davis.In sys/uio.h 468269e767SBrooks Davis.Ft ssize_t 478269e767SBrooks Davis.Fn readv "int fd" "const struct iovec *iov" "int iovcnt" 488269e767SBrooks Davis.Ft ssize_t 498269e767SBrooks Davis.Fn preadv "int fd" "const struct iovec *iov" "int iovcnt" "off_t offset" 508269e767SBrooks Davis.Sh DESCRIPTION 518269e767SBrooks DavisThe 528269e767SBrooks Davis.Fn read 538269e767SBrooks Davissystem call 548269e767SBrooks Davisattempts to read 558269e767SBrooks Davis.Fa nbytes 568269e767SBrooks Davisof data from the object referenced by the descriptor 578269e767SBrooks Davis.Fa fd 588269e767SBrooks Davisinto the buffer pointed to by 598269e767SBrooks Davis.Fa buf . 608269e767SBrooks DavisThe 618269e767SBrooks Davis.Fn readv 628269e767SBrooks Davissystem call 638269e767SBrooks Davisperforms the same action, but scatters the input data 648269e767SBrooks Davisinto the 658269e767SBrooks Davis.Fa iovcnt 668269e767SBrooks Davisbuffers specified by the members of the 678269e767SBrooks Davis.Fa iov 688269e767SBrooks Davisarray: iov[0], iov[1], ..., iov[iovcnt\|\-\|1]. 698269e767SBrooks DavisThe 708269e767SBrooks Davis.Fn pread 718269e767SBrooks Davisand 728269e767SBrooks Davis.Fn preadv 738269e767SBrooks Davissystem calls 748269e767SBrooks Davisperform the same functions, but read from the specified position in 758269e767SBrooks Davisthe file without modifying the file pointer. 768269e767SBrooks Davis.Pp 778269e767SBrooks DavisFor 788269e767SBrooks Davis.Fn readv 798269e767SBrooks Davisand 808269e767SBrooks Davis.Fn preadv , 818269e767SBrooks Davisthe 828269e767SBrooks Davis.Fa iovec 838269e767SBrooks Davisstructure is defined as: 848269e767SBrooks Davis.Pp 858269e767SBrooks Davis.Bd -literal -offset indent -compact 868269e767SBrooks Davisstruct iovec { 878269e767SBrooks Davis void *iov_base; /* Base address. */ 888269e767SBrooks Davis size_t iov_len; /* Length. */ 898269e767SBrooks Davis}; 908269e767SBrooks Davis.Ed 918269e767SBrooks Davis.Pp 928269e767SBrooks DavisEach 938269e767SBrooks Davis.Fa iovec 948269e767SBrooks Davisentry specifies the base address and length of an area 958269e767SBrooks Davisin memory where data should be placed. 968269e767SBrooks DavisThe 978269e767SBrooks Davis.Fn readv 988269e767SBrooks Davissystem call 998269e767SBrooks Daviswill always fill an area completely before proceeding 1008269e767SBrooks Davisto the next. 1018269e767SBrooks Davis.Pp 1028269e767SBrooks DavisOn objects capable of seeking, the 1038269e767SBrooks Davis.Fn read 1048269e767SBrooks Davisstarts at a position 1058269e767SBrooks Davisgiven by the pointer associated with 1068269e767SBrooks Davis.Fa fd 1078269e767SBrooks Davis(see 1088269e767SBrooks Davis.Xr lseek 2 ) . 1098269e767SBrooks DavisUpon return from 1108269e767SBrooks Davis.Fn read , 1118269e767SBrooks Davisthe pointer is incremented by the number of bytes actually read. 1128269e767SBrooks Davis.Pp 1138269e767SBrooks DavisObjects that are not capable of seeking always read from the current 1148269e767SBrooks Davisposition. 1158269e767SBrooks DavisThe value of the pointer associated with such an 1168269e767SBrooks Davisobject is undefined. 1178269e767SBrooks Davis.Pp 1188269e767SBrooks DavisUpon successful completion, 1198269e767SBrooks Davis.Fn read , 1208269e767SBrooks Davis.Fn readv , 1218269e767SBrooks Davis.Fn pread 1228269e767SBrooks Davisand 1238269e767SBrooks Davis.Fn preadv 1248269e767SBrooks Davisreturn the number of bytes actually read and placed in the buffer. 1258269e767SBrooks DavisThe system guarantees to read the number of bytes requested if 1268269e767SBrooks Davisthe descriptor references a normal file that has that many bytes left 1278269e767SBrooks Davisbefore the end-of-file, but in no other case. 1288269e767SBrooks Davis.Pp 1298269e767SBrooks DavisIn accordance with 1308269e767SBrooks Davis.St -p1003.1-2004 , 1318269e767SBrooks Davisboth 1320d161f7bSChristopher Davidson.Fn read 1338269e767SBrooks Davisand 1340d161f7bSChristopher Davidson.Fn write 1358269e767SBrooks Davissyscalls are atomic with respect to each other in the effects on file 1368269e767SBrooks Daviscontent, when they operate on regular files. 1378269e767SBrooks DavisIf two threads each call one of the 1380d161f7bSChristopher Davidson.Fn read 1398269e767SBrooks Davisor 1400d161f7bSChristopher Davidson.Fn write , 1418269e767SBrooks Davissyscalls, each call will see either all of the changes of the other call, 1428269e767SBrooks Davisor none of them. 1438269e767SBrooks DavisThe 1448269e767SBrooks Davis.Fx 1458269e767SBrooks Daviskernel implements this guarantee by locking the file ranges affected by 1468269e767SBrooks Davisthe calls. 1478269e767SBrooks Davis.Sh RETURN VALUES 1488269e767SBrooks DavisIf successful, the 1498269e767SBrooks Davisnumber of bytes actually read is returned. 1508269e767SBrooks DavisUpon reading end-of-file, 1518269e767SBrooks Daviszero is returned. 1528269e767SBrooks DavisOtherwise, a -1 is returned and the global variable 1538269e767SBrooks Davis.Va errno 1548269e767SBrooks Davisis set to indicate the error. 1558269e767SBrooks Davis.Sh ERRORS 1568269e767SBrooks DavisThe 1578269e767SBrooks Davis.Fn read , 1588269e767SBrooks Davis.Fn readv , 1598269e767SBrooks Davis.Fn pread 1608269e767SBrooks Davisand 1618269e767SBrooks Davis.Fn preadv 1628269e767SBrooks Davissystem calls 1638269e767SBrooks Daviswill succeed unless: 1648269e767SBrooks Davis.Bl -tag -width Er 1658269e767SBrooks Davis.It Bq Er EBADF 1668269e767SBrooks DavisThe 1678269e767SBrooks Davis.Fa fd 1688269e767SBrooks Davisargument 1698269e767SBrooks Davisis not a valid file or socket descriptor open for reading. 1708269e767SBrooks Davis.It Bq Er ECONNRESET 1718269e767SBrooks DavisThe 1728269e767SBrooks Davis.Fa fd 1738269e767SBrooks Davisargument refers to a socket, and the remote socket end is 1748269e767SBrooks Davisforcibly closed. 1758269e767SBrooks Davis.It Bq Er EFAULT 1768269e767SBrooks DavisThe 1778269e767SBrooks Davis.Fa buf 1788269e767SBrooks Davisargument 1798269e767SBrooks Davispoints outside the allocated address space. 1808269e767SBrooks Davis.It Bq Er EIO 1818269e767SBrooks DavisAn I/O error occurred while reading from the file system. 1828269e767SBrooks Davis.It Bq Er EINTEGRITY 1838269e767SBrooks DavisCorrupted data was detected while reading from the file system. 1848269e767SBrooks Davis.It Bq Er EBUSY 1858269e767SBrooks DavisFailed to read from a file, e.g. /proc/<pid>/regs while <pid> is not stopped 1868269e767SBrooks Davis.It Bq Er EINTR 1878269e767SBrooks DavisA read from a slow device 1888269e767SBrooks Davis(i.e.\& one that might block for an arbitrary amount of time) 1898269e767SBrooks Daviswas interrupted by the delivery of a signal 1908269e767SBrooks Davisbefore any data arrived. 1918269e767SBrooks Davis.It Bq Er EINVAL 1928269e767SBrooks DavisThe pointer associated with 1938269e767SBrooks Davis.Fa fd 1948269e767SBrooks Daviswas negative. 1958269e767SBrooks Davis.It Bq Er EAGAIN 1968269e767SBrooks DavisThe file was marked for non-blocking I/O, 1978269e767SBrooks Davisand no data were ready to be read. 1988269e767SBrooks Davis.It Bq Er EISDIR 1998269e767SBrooks DavisThe file descriptor is associated with a directory. 2008269e767SBrooks DavisDirectories may only be read directly by root if the filesystem supports it and 2018269e767SBrooks Davisthe 2028269e767SBrooks Davis.Dv security.bsd.allow_read_dir 2038269e767SBrooks Davissysctl MIB is set to a non-zero value. 2048269e767SBrooks DavisFor most scenarios, the 2058269e767SBrooks Davis.Xr readdir 3 2068269e767SBrooks Davisfunction should be used instead. 2078269e767SBrooks Davis.It Bq Er EOPNOTSUPP 2088269e767SBrooks DavisThe file descriptor is associated with a file system and file type that 2098269e767SBrooks Davisdo not allow regular read operations on it. 2108269e767SBrooks Davis.It Bq Er EOVERFLOW 2118269e767SBrooks DavisThe file descriptor is associated with a regular file, 2128269e767SBrooks Davis.Fa nbytes 2138269e767SBrooks Davisis greater than 0, 2148269e767SBrooks Davis.Fa offset 2158269e767SBrooks Davisis before the end-of-file, and 2168269e767SBrooks Davis.Fa offset 2178269e767SBrooks Davisis greater than or equal to the offset maximum established 2188269e767SBrooks Davisfor this file system. 2198269e767SBrooks Davis.It Bq Er EINVAL 2208269e767SBrooks DavisThe value 2218269e767SBrooks Davis.Fa nbytes 2228269e767SBrooks Davisis greater than 2233e951584SKonstantin Belousov.Dv SSIZE_MAX 2243e951584SKonstantin Belousov(or greater than 2253e951584SKonstantin Belousov.Dv INT_MAX , 2263e951584SKonstantin Belousovif the sysctl 2273e951584SKonstantin Belousov.Va debug.iosize_max_clamp 2283e951584SKonstantin Belousovis non-zero). 2298269e767SBrooks Davis.El 2308269e767SBrooks Davis.Pp 2318269e767SBrooks DavisIn addition, 2328269e767SBrooks Davis.Fn readv 2338269e767SBrooks Davisand 2348269e767SBrooks Davis.Fn preadv 2358269e767SBrooks Davismay return one of the following errors: 2368269e767SBrooks Davis.Bl -tag -width Er 2378269e767SBrooks Davis.It Bq Er EINVAL 2388269e767SBrooks DavisThe 2398269e767SBrooks Davis.Fa iovcnt 2408269e767SBrooks Davisargument 2418269e767SBrooks Daviswas less than or equal to 0, or greater than 2428269e767SBrooks Davis.Dv IOV_MAX . 2438269e767SBrooks Davis.It Bq Er EINVAL 2448269e767SBrooks DavisOne of the 2458269e767SBrooks Davis.Fa iov_len 2468269e767SBrooks Davisvalues in the 2478269e767SBrooks Davis.Fa iov 2488269e767SBrooks Davisarray was negative. 2498269e767SBrooks Davis.It Bq Er EINVAL 2508269e767SBrooks DavisThe sum of the 2518269e767SBrooks Davis.Fa iov_len 2528269e767SBrooks Davisvalues in the 2538269e767SBrooks Davis.Fa iov 2543e951584SKonstantin Belousovarray is greater than 2553e951584SKonstantin Belousov.Dv SSIZE_MAX 2563e951584SKonstantin Belousov(or greater than 2573e951584SKonstantin Belousov.Dv INT_MAX , 2583e951584SKonstantin Belousovif the sysctl 2593e951584SKonstantin Belousov.Va debug.iosize_max_clamp 2603e951584SKonstantin Belousovis non-zero). 2618269e767SBrooks Davis.It Bq Er EFAULT 2628269e767SBrooks DavisPart of the 2638269e767SBrooks Davis.Fa iov 2648269e767SBrooks Davisarray points outside the process's allocated address space. 2658269e767SBrooks Davis.El 2668269e767SBrooks Davis.Pp 2678269e767SBrooks DavisThe 2688269e767SBrooks Davis.Fn pread 2698269e767SBrooks Davisand 2708269e767SBrooks Davis.Fn preadv 2718269e767SBrooks Davissystem calls may also return the following errors: 2728269e767SBrooks Davis.Bl -tag -width Er 2738269e767SBrooks Davis.It Bq Er EINVAL 2748269e767SBrooks DavisThe 2758269e767SBrooks Davis.Fa offset 2768269e767SBrooks Davisvalue was negative. 2778269e767SBrooks Davis.It Bq Er ESPIPE 2788269e767SBrooks DavisThe file descriptor is associated with a pipe, socket, or FIFO. 2798269e767SBrooks Davis.El 2808269e767SBrooks Davis.Sh SEE ALSO 2818269e767SBrooks Davis.Xr dup 2 , 2828269e767SBrooks Davis.Xr fcntl 2 , 2838269e767SBrooks Davis.Xr getdirentries 2 , 2848269e767SBrooks Davis.Xr open 2 , 2858269e767SBrooks Davis.Xr pipe 2 , 2868269e767SBrooks Davis.Xr select 2 , 2878269e767SBrooks Davis.Xr socket 2 , 2888269e767SBrooks Davis.Xr socketpair 2 , 289*beadbca6SWarner Losh.Xr write 2 , 2908269e767SBrooks Davis.Xr fread 3 , 2918269e767SBrooks Davis.Xr readdir 3 2928269e767SBrooks Davis.Sh STANDARDS 2938269e767SBrooks DavisThe 2948269e767SBrooks Davis.Fn read 2958269e767SBrooks Davissystem call is expected to conform to 2968269e767SBrooks Davis.St -p1003.1-90 . 2978269e767SBrooks DavisThe 2988269e767SBrooks Davis.Fn readv 2998269e767SBrooks Davisand 3008269e767SBrooks Davis.Fn pread 3018269e767SBrooks Davissystem calls are expected to conform to 3028269e767SBrooks Davis.St -xpg4.2 . 3038269e767SBrooks Davis.Sh HISTORY 3048269e767SBrooks DavisThe 3058269e767SBrooks Davis.Fn preadv 3068269e767SBrooks Davissystem call appeared in 3078269e767SBrooks Davis.Fx 6.0 . 3088269e767SBrooks DavisThe 3098269e767SBrooks Davis.Fn pread 3108269e767SBrooks Davisfunction appeared in 3118269e767SBrooks Davis.At V.4 . 3128269e767SBrooks DavisThe 3138269e767SBrooks Davis.Fn readv 3148269e767SBrooks Davissystem call appeared in 3158269e767SBrooks Davis.Bx 4.2 . 3168269e767SBrooks DavisThe 3178269e767SBrooks Davis.Fn read 3188269e767SBrooks Davisfunction appeared in 3198269e767SBrooks Davis.At v1 . 320