1read(2)                          System Calls                          read(2)
2
3
4

NAME

6       read, readv, pread - read from file
7

SYNOPSIS

9       #include <unistd.h>
10
11       ssize_t read(int fildes, void *buf, size_t nbyte);
12
13
14       ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset);
15
16
17       #include <sys/uio.h>
18
19       ssize_t readv(int fildes, const struct iovec *iov, int iovcnt);
20
21

DESCRIPTION

23       The  read() function attempts to read nbyte bytes from the file associ‐
24       ated with the open file descriptor, fildes, into the buffer pointed  to
25       by buf.
26
27
28       If nbyte is 0, read() returns 0 and has no other results.
29
30
31       On files that support seeking (for example, a regular file), the read()
32       starts at a position in the file given by the  file  offset  associated
33       with  fildes.  The  file  offset  is incremented by the number of bytes
34       actually read.
35
36
37       Files that do not support seeking (for example, terminals) always  read
38       from  the  current position. The value of a file offset associated with
39       such a file is undefined.
40
41
42       If fildes refers to a socket, read()  is  equivalent  to  recv(3SOCKET)
43       with no flags set.
44
45
46       No  data  transfer  will  occur  past  the current end-of-file.  If the
47       starting position is at or after the end-of-file, 0 will  be  returned.
48       If  the  file refers to a device special file, the result of subsequent
49       read() requests is implementation-dependent.
50
51
52       When attempting to read from a regular file with mandatory  file/record
53       locking  set (see chmod(2)), and there is a write lock owned by another
54       process on the segment of the file to be read:
55
56           o      If O_NDELAY or O_NONBLOCK is set, read() returns −1 and sets
57                  errno to EAGAIN.
58
59           o      If  O_NDELAY  and  O_NONBLOCK are clear, read() sleeps until
60                  the blocking record lock is removed.
61
62
63       When attempting to read from an empty pipe (or FIFO):
64
65           o      If no process has the pipe open for writing, read()  returns
66                  0 to indicate end-of-file.
67
68           o      If  some  process has the pipe open for writing and O_NDELAY
69                  is set, read() returns 0.
70
71           o      If some process has the pipe open for writing and O_NONBLOCK
72                  is set, read() returns −1 and sets errno to EAGAIN.
73
74           o      If  O_NDELAY  and  O_NONBLOCK are clear, read() blocks until
75                  data is written to the pipe or the pipe  is  closed  by  all
76                  processes that had opened the pipe for writing.
77
78
79       When  attempting  to read a file associated with a terminal that has no
80       data currently available:
81
82           o      If O_NDELAY is set, read() returns 0.
83
84           o      If O_NONBLOCK is set, read() returns −1 and  sets  errno  to
85                  EAGAIN.
86
87           o      If  O_NDELAY  and  O_NONBLOCK are clear, read() blocks until
88                  data become available.
89
90
91       When attempting to read a file associated with a  socket  or  a  stream
92       that  is  not  a pipe, a FIFO, or a terminal,  and the file has no data
93       currently available:
94
95           o      If O_NDELAY or O_NONBLOCK is set, read() returns −1 and sets
96                  errno to EAGAIN.
97
98           o      If  O_NDELAY  and  O_NONBLOCK are clear, read() blocks until
99                  data becomes available.
100
101
102       The read() function reads data previously written to a  file.   If  any
103       portion  of  a regular file prior to the end-of-file has not been writ‐
104       ten, read() returns bytes with value 0.  For example,  lseek(2)  allows
105       the  file offset to be set beyond the end of existing data in the file.
106       If data is later written at this point, subsequent  reads  in  the  gap
107       between the previous end of data and the newly written data will return
108       bytes with value 0 until data is written into the gap.
109
110
111       For regular files, no data transfer will occur past the offset  maximum
112       established in the open file description associated with fildes.
113
114
115       Upon  successful completion, where nbyte is greater than 0, read() will
116       mark for update the st_atime field of the file, and return  the  number
117       of  bytes read. This number will never be greater than nbyte. The value
118       returned may be less than nbyte if the number of bytes left in the file
119       is  less than nbyte, if the read() request was interrupted by a signal,
120       or if the file is a pipe or FIFO or special file  and  has  fewer  than
121       nbyte  bytes  immediately available for reading.  For example, a read()
122       from a file associated with a terminal may return  one  typed  line  of
123       data.
124
125
126       If  a  read()  is  interrupted by a signal before it reads any data, it
127       will return −1 with errno set to EINTR.
128
129
130       If a read() is interrupted by a signal after it has  successfully  read
131       some data, it will return the number of bytes read.
132
133
134       A  read()  from  a streams file can read data in three different modes:
135       byte-stream mode, message-nondiscard mode,  and  message-discard  mode.
136       The  default  is  byte-stream  mode.   This  can  be  changed using the
137       I_SRDOPT ioctl(2) request, and can be tested with the I_GRDOPT ioctl().
138       In  byte-stream  mode,  read()  retrieves data from the stream until as
139       many bytes as were requested are transferred, or until there is no more
140       data to be retrieved.  Byte-stream mode ignores message boundaries.
141
142
143       In streams message-nondiscard mode, read() retrieves data until as many
144       bytes as were requested are transferred, or until a message boundary is
145       reached.   If  read()  does not retrieve all the data in a message, the
146       remaining data is left on the stream, and can be retrieved by the  next
147       read()  call.   Message-discard  mode also retrieves data until as many
148       bytes as were requested are  transferred,  or  a  message  boundary  is
149       reached.  However, unread data remaining in a message after the  read()
150       returns is discarded, and is not available  for  a  subsequent  read(),
151       readv() or getmsg(2) call.
152
153
154       How read() handles zero-byte streams messages is determined by the cur‐
155       rent read mode setting.  In byte-stream mode, read() accepts data until
156       it  has  read  nbyte  bytes, or until there is no more data to read, or
157       until a zero-byte message block is  encountered.  The  read()  function
158       then returns the number of bytes read, and places the zero-byte message
159       back on the stream to be retrieved  by  the  next  read(),  readv()  or
160       getmsg(2).  In message-nondiscard mode or message-discard mode, a zero-
161       byte message returns 0 and the message  is  removed  from  the  stream.
162       When  a zero-byte message is read as the first message on a stream, the
163       message is removed from the stream and 0 is returned, regardless of the
164       read mode.
165
166
167       A  read()  from  a  streams file returns the data in the message at the
168       front of the stream head read queue, regardless of the priority band of
169       the message.
170
171
172       By  default, streams are in control-normal mode, in which a read() from
173       a streams file can only process messages that contain a data  part  but
174       do  not contain a control part.  The read() fails if a message contain‐
175       ing a control part is encountered at the  stream  head.   This  default
176       action can be changed by placing the stream in either control-data mode
177       or control-discard mode with the I_SRDOPT ioctl() command.  In control-
178       data  mode,  read()  converts any control part to data and passes it to
179       the application before passing any data part originally present in  the
180       same message.  In control-discard mode, read() discards message control
181       parts but returns to the process any data part in the message.
182
183
184       In addition, read() and readv() will fail if the stream head  had  pro‐
185       cessed  an asynchronous error before the call.  In this case, the value
186       of errno does not reflect the result of read() or readv() but  reflects
187       the  prior  error.  If a hangup occurs on the stream being read, read()
188       continues to operate normally until  the  stream  head  read  queue  is
189       empty. Thereafter, it returns 0.
190
191   readv()
192       The readv() function is equivalent to read(), but places the input data
193       into the iovcnt buffers specified by the  members  of  the  iov  array:
194       iov[0],  iov[1],  ...,  iov[iovcnt−1].  The iovcnt argument is valid if
195       greater than 0 and less than or equal to {IOV_MAX}.
196
197
198       The iovec structure contains the following members:
199
200         caddr_t   iov_base;
201         int       iov_len;
202
203
204
205       Each iovec entry specifies the base address and length of  an  area  in
206       memory  where data should be placed.  The readv() function always fills
207       an area completely before proceeding to the next.
208
209
210       Upon successful completion, readv() marks for update the st_atime field
211       of the file.
212
213   pread()
214       The pread() function performs the same action as read(), except that it
215       reads from a given position in  the  file  without  changing  the  file
216       pointer.  The  first  three arguments to pread() are the same as read()
217       with the addition of a fourth argument offset for the desired  position
218       inside  the file. pread() will read up to the maximum offset value that
219       can be represented in an off_t for regular files. An attempt to perform
220       a pread() on a file that is incapable of seeking results in an error.
221

RETURN VALUES

223       Upon  successful  completion,  read() and readv() return a non-negative
224       integer indicating the number of bytes actually  read.  Otherwise,  the
225       functions return −1 and set errno to indicate the error.
226

ERRORS

228       The read(), readv(), and pread() functions will fail if:
229
230       EAGAIN     Mandatory  file/record  locking  was set, O_NDELAY or O_NON‐
231                  BLOCK was set, and there was a blocking record  lock;  total
232                  amount of system memory available when reading using raw I/O
233                  is temporarily insufficient; no data is waiting to  be  read
234                  on  a  file  associated with a tty device and O_NONBLOCK was
235                  set; or no message is waiting to be read  on  a  stream  and
236                  O_NDELAY or O_NONBLOCK was set.
237
238
239       EBADF      The  fildes argument is not a valid file descriptor open for
240                  reading.
241
242
243       EBADMSG    Message waiting to be read on a stream is not  a  data  mes‐
244                  sage.
245
246
247       EDEADLK    The  read  was  going to go to sleep and cause a deadlock to
248                  occur.
249
250
251       EINTR      A signal was caught during the read operation  and  no  data
252                  was transferred.
253
254
255       EINVAL     An attempt was made to read from a stream linked to a multi‐
256                  plexor.
257
258
259       EIO        A physical I/O error has occurred, or the process  is  in  a
260                  background  process group and is attempting to read from its
261                  controlling terminal, and either the process is ignoring  or
262                  blocking  the  SIGTTIN  signal  or  the process group of the
263                  process is orphaned.
264
265
266       EISDIR     The fildes argument refers to a directory on a  file  system
267                  type that does not support read operations on directories.
268
269
270       ENOLCK     The  system  record  lock  table  was full, so the read() or
271                  readv() could not go to sleep until the blocking record lock
272                  was removed.
273
274
275       ENOLINK    The  fildes  argument is on a remote machine and the link to
276                  that machine is no longer active.
277
278
279       ENXIO      The device associated with fildes  is  a  block  special  or
280                  character  special file and the value of the file pointer is
281                  out of range.
282
283
284
285       The read() and pread() functions will fail if:
286
287       EFAULT    The buf argument points to an illegal address.
288
289
290       EINVAL    The nbyte argument overflowed an ssize_t.
291
292
293
294       The read() and readv() functions will fail if:
295
296       EOVERFLOW    The file is a regular file, nbyte is greater than  0,  the
297                    starting  position  is  before  the  end-of-file,  and the
298                    starting position is greater than or equal to  the  offset
299                    maximum  established  in the open file description associ‐
300                    ated with fildes.
301
302
303
304       The readv() function may fail if:
305
306       EFAULT    The iov argument points outside the allocated address space.
307
308
309       EINVAL    The iovcnt argument was less than or equal to  0  or  greater
310                 than {IOV_MAX}. See Intro(2) for a definition of {IOV_MAX}).
311
312                 One  of  the iov_len values in the iov array was negative, or
313                 the sum of the iov_len values in the iov array overflowed  an
314                 ssize_t.
315
316
317
318       The  pread()  function  will fail and the file pointer remain unchanged
319       if:
320
321       ESPIPE    The fildes argument is associated with a pipe or FIFO.
322
323

USAGE

325       The pread() function has a transitional interface for 64-bit file  off‐
326       sets.  See lf64(5).
327

ATTRIBUTES

329       See attributes(5) for descriptions of the following attributes:
330
331
332
333
334       ┌─────────────────────────────┬─────────────────────────────┐
335       │      ATTRIBUTE TYPE         │      ATTRIBUTE VALUE        │
336       ├─────────────────────────────┼─────────────────────────────┤
337       │Interface Stability          │Committed                    │
338       ├─────────────────────────────┼─────────────────────────────┤
339       │MT-Level                     │read() is Async-Signal-Safe  │
340       ├─────────────────────────────┼─────────────────────────────┤
341       │Standard                     │See standards(5).            │
342       └─────────────────────────────┴─────────────────────────────┘
343

SEE ALSO

345       Intro(2),  chmod(2),  creat(2),  dup(2), fcntl(2), getmsg(2), ioctl(2),
346       lseek(2),  open(2),  pipe(2),  recv(3SOCKET),  attributes(5),  lf64(5),
347       standards(5), streamio(7I), termio(7I)
348
349
350
351SunOS 5.11                        13 Sep 2007                          read(2)
Impressum