read.2 7.3 KB

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