summaryrefslogtreecommitdiff
path: root/usr/src/man/man2/read.2
blob: 2d3cda107c09b5b85c6e50136c5ef0ea2100a80e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
.\"
.\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
.\" permission to reproduce portions of its copyrighted documentation.
.\" Original documentation from The Open Group can be obtained online at
.\" http://www.opengroup.org/bookstore/.
.\"
.\" The Institute of Electrical and Electronics Engineers and The Open
.\" Group, have given us permission to reprint portions of their
.\" documentation.
.\"
.\" In the following statement, the phrase ``this text'' refers to portions
.\" of the system documentation.
.\"
.\" Portions of this text are reprinted and reproduced in electronic form
.\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
.\" Standard for Information Technology -- Portable Operating System
.\" Interface (POSIX), The Open Group Base Specifications Issue 6,
.\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
.\" Engineers, Inc and The Open Group.  In the event of any discrepancy
.\" between these versions and the original IEEE and The Open Group
.\" Standard, the original IEEE and The Open Group Standard is the referee
.\" document.  The original Standard can be obtained online at
.\" http://www.opengroup.org/unix/online.html.
.\"
.\" This notice shall appear on any product containing this material.
.\"
.\" The contents of this file are subject to the terms of the
.\" Common Development and Distribution License (the "License").
.\" You may not use this file except in compliance with the License.
.\"
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
.\" or http://www.opensolaris.org/os/licensing.
.\" See the License for the specific language governing permissions
.\" and limitations under the License.
.\"
.\" When distributing Covered Code, include this CDDL HEADER in each
.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
.\" If applicable, add the following below this CDDL HEADER, with the
.\" fields enclosed by brackets "[]" replaced with your own identifying
.\" information: Portions Copyright [yyyy] [name of copyright owner]
.\"
.\"
.\" Copyright 1989 AT&T
.\" Portions Copyright (c) 1992, X/Open Company Limited.  All Rights Reserved.
.\" Copyright (c) 2007, Sun Microsystems, Inc.  All Rights Reserved.
.\" Copyright (c) 2018, Joyent, Inc.
.\"
.TH READ 2 "September 10, 2018"
.SH NAME
read, readv, pread, preadv \- read from file
.SH SYNOPSIS
.LP
.nf
#include <unistd.h>

\fBssize_t\fR \fBread\fR(\fBint\fR \fIfildes\fR, \fBvoid *\fR\fIbuf\fR, \fBsize_t\fR \fInbyte\fR);
.fi

.LP
.nf
\fBssize_t\fR \fBpread\fR(\fBint\fR \fIfildes\fR, \fBvoid *\fR\fIbuf\fR, \fBsize_t\fR \fInbyte\fR, \fBoff_t\fR \fIoffset\fR);
.fi

.LP
.nf
#include <sys/uio.h>

\fBssize_t\fR \fBreadv\fR(\fBint\fR \fIfildes\fR, \fBconst struct iovec *\fR\fIiov\fR, \fBint\fR \fIiovcnt\fR);
.fi
.LP
.nf
\fBssize_t\fR \fBpreadv\fR(\fBint\fR \fIfildes\fR, \fBconst struct iovec *\fR\fIiov\fR, \fBint\fR \fIiovcnt\fR, \fBoff_t\fR \fIoffset\fR);
.fi


.SH DESCRIPTION
.LP
The \fBread()\fR function attempts to read \fInbyte\fR bytes from the file
associated with the open file descriptor, \fIfildes\fR, into the buffer pointed
to by \fIbuf\fR.
.sp
.LP
If \fInbyte\fR is 0, \fBread()\fR returns \fB0\fR and has no other results.
.sp
.LP
On files that support seeking (for example, a regular file), the \fBread()\fR
starts at a position in the file given by the file offset associated with
\fIfildes\fR. The file offset is incremented by the number of bytes actually
read.
.sp
.LP
Files that do not support seeking (for example, terminals) always read from the
current position. The value of a file offset associated with such a file is
undefined.
.sp
.LP
If \fIfildes\fR refers to a socket, \fBread()\fR is equivalent to
\fBrecv\fR(3SOCKET) with no flags set.
.sp
.LP
No data transfer will occur past the current end-of-file.  If the starting
position is at or after the end-of-file, \fB0\fR will be returned. If the file
refers to a device special file, the result of subsequent \fBread()\fR requests
is implementation-dependent.
.sp
.LP
When attempting to read from a regular file with mandatory file/record locking
set (see \fBchmod\fR(2)), and there is a write lock owned by another process on
the segment of the file to be read:
.RS +4
.TP
.ie t \(bu
.el o
If \fBO_NDELAY\fR or \fBO_NONBLOCK\fR is set, \fBread()\fR returns \fB\(mi1\fR
and sets \fBerrno\fR to \fBEAGAIN\fR.
.RE
.RS +4
.TP
.ie t \(bu
.el o
If \fBO_NDELAY\fR and \fBO_NONBLOCK\fR are clear, \fBread()\fR sleeps until the
blocking record lock is removed.
.RE
.sp
.LP
When attempting to read from an empty pipe (or FIFO):
.RS +4
.TP
.ie t \(bu
.el o
If no process has the pipe open for writing, \fBread()\fR returns \fB0\fR to
indicate end-of-file.
.RE
.RS +4
.TP
.ie t \(bu
.el o
If some process has the pipe open for writing and \fBO_NDELAY\fR is set,
\fBread()\fR returns \fB0\fR.
.RE
.RS +4
.TP
.ie t \(bu
.el o
If some process has the pipe open for writing and \fBO_NONBLOCK\fR is set,
\fBread()\fR returns \fB\(mi1\fR and sets \fBerrno\fR to \fBEAGAIN\fR.
.RE
.RS +4
.TP
.ie t \(bu
.el o
If \fBO_NDELAY\fR and \fBO_NONBLOCK\fR are clear, \fBread()\fR blocks until
data is written to the pipe or the pipe is closed by all processes that had
opened the pipe for writing.
.RE
.sp
.LP
When attempting to read a file associated with a terminal that has no data
currently available:
.RS +4
.TP
.ie t \(bu
.el o
If \fBO_NDELAY\fR is set, \fBread()\fR returns \fB0\fR.
.RE
.RS +4
.TP
.ie t \(bu
.el o
If \fBO_NONBLOCK\fR is set, \fBread()\fR returns \fB\(mi1\fR and sets
\fBerrno\fR to \fBEAGAIN\fR.
.RE
.RS +4
.TP
.ie t \(bu
.el o
If \fBO_NDELAY\fR and \fBO_NONBLOCK\fR are clear, \fBread()\fR blocks until
data become available.
.RE
.sp
.LP
When attempting to read a file associated with a socket or a stream that is not
a pipe, a FIFO, or a terminal,  and the file has no data currently available:
.RS +4
.TP
.ie t \(bu
.el o
If \fBO_NDELAY\fR or \fBO_NONBLOCK\fR is set, \fBread()\fR returns \fB\(mi1\fR
and sets \fBerrno\fR to \fBEAGAIN\fR.
.RE
.RS +4
.TP
.ie t \(bu
.el o
If \fBO_NDELAY\fR and \fBO_NONBLOCK\fR are clear, \fBread()\fR blocks until
data becomes available.
.RE
.sp
.LP
The \fBread()\fR function reads data previously written to a file.  If any
portion of a regular file prior to the end-of-file has not been written,
\fBread()\fR returns bytes with value 0.  For example, \fBlseek\fR(2) allows
the file offset to be set beyond the end of existing data in the file. If data
is later written at this point, subsequent reads in the gap between the
previous end of data and the newly written data will return bytes with value 0
until data is written into the gap.
.sp
.LP
For regular files, no data transfer will occur past the offset maximum
established in the open file description associated with \fIfildes\fR.
.sp
.LP
Upon successful completion, where \fInbyte\fR is greater than 0, \fBread()\fR
will mark for update the \fBst_atime\fR field of the file, and return the
number of bytes read. This number will never be greater than \fInbyte\fR. The
value returned may be less than \fInbyte\fR if the number of bytes left in the
file is less than \fInbyte\fR, if the \fBread()\fR request was interrupted by a
signal, or if the file is a pipe or FIFO or special file and has fewer than
\fInbyte\fR bytes immediately available for reading.  For example, a
\fBread()\fR from a file associated with a terminal may return one typed line
of data.
.sp
.LP
If a \fBread()\fR is interrupted by a signal before it reads any data, it will
return \fB\(mi1\fR with \fBerrno\fR set to \fBEINTR\fR.
.sp
.LP
If a \fBread()\fR is interrupted by a signal after it has successfully read
some data, it will return the number of bytes read.
.sp
.LP
A \fBread()\fR from a streams file can read data in three different modes:
byte-stream mode, message-nondiscard mode, and message-discard mode.  The
default is byte-stream mode.  This can be changed using the \fBI_SRDOPT\fR
\fBioctl\fR(2) request, and can be tested with the \fBI_GRDOPT\fR
\fBioctl()\fR. In byte-stream mode, \fBread()\fR retrieves data from the stream
until as many bytes as were requested are transferred, or until there is no
more data to be retrieved.  Byte-stream mode ignores message boundaries.
.sp
.LP
In streams message-nondiscard mode, \fBread()\fR retrieves data until as many
bytes as were requested are transferred, or until a message boundary is
reached.  If \fBread()\fR does not retrieve all the data in a message, the
remaining data is left on the stream, and can be retrieved by the next
\fBread()\fR call.  Message-discard mode also retrieves data until as many
bytes as were requested are transferred, or a message boundary is reached.
However, unread data remaining in a message after the  \fBread()\fR returns is
discarded, and is not available for a subsequent \fBread()\fR, \fBreadv()\fR or
\fBgetmsg\fR(2) call.
.sp
.LP
How \fBread()\fR handles zero-byte streams messages is determined by the
current read mode setting.  In byte-stream mode, \fBread()\fR accepts data
until it has read \fInbyte\fR bytes, or until there is no more data to read, or
until a zero-byte message block is encountered. The \fBread()\fR function then
returns the number of bytes read, and places the zero-byte message back on the
stream to be retrieved by the next \fBread()\fR, \fBreadv()\fR or
\fBgetmsg\fR(2). In message-nondiscard mode or message-discard mode, a
zero-byte message returns \fB0\fR and the message is removed from the stream.
When a zero-byte message is read as the first message on a stream, the message
is removed from the stream and \fB0\fR is returned, regardless of the read
mode.
.sp
.LP
A \fBread()\fR from a streams file returns the data in the message at the front
of the stream head read queue, regardless of the priority band of the message.
.sp
.LP
By default, streams are in control-normal mode, in which a \fBread()\fR from a
streams file can only process messages that contain a data part but do not
contain a control part.  The \fBread()\fR fails if a message containing a
control part is encountered at the stream head.  This default action can be
changed by placing the stream in either control-data mode or control-discard
mode with the \fBI_SRDOPT\fR \fBioctl()\fR command.  In control-data mode,
\fBread()\fR converts any control part to data and passes it to the application
before passing any data part originally present in the same message.  In
control-discard mode, \fBread()\fR discards message control parts but returns
to the process any data part in the message.
.sp
.LP
In addition, \fBread()\fR and \fBreadv()\fR will fail if the stream head had
processed an asynchronous error before the call.  In this case, the value of
\fBerrno\fR does not reflect the result of \fBread()\fR or \fBreadv()\fR but
reflects the prior error. If a hangup occurs on the stream being read,
\fBread()\fR continues to operate normally until the stream head read queue is
empty. Thereafter, it returns \fB0\fR.
.sp
.LP
If an asynchronous error occurs on a socket, it is possible for the \fBread()\fR
and \fBreadv()\fR functions to return an asynchronous error, just as in the
STREAMS case described above. This might occur, for example, if a TCP socket
that is using TCP keep-alive is closed due to failing the keep-alive check.
.SS "\fBreadv()\fR"
.LP
The \fBreadv()\fR function is equivalent to \fBread()\fR, but places the input
data into the \fIiovcnt\fR buffers specified by the members of the \fIiov\fR
array: \fIiov\fR[\fB0\fR], \fIiov\fR[\fB1\fR], \&.\|.\|.,
\fIiov\fR[\fIiovcnt\fR\(mi1]. The \fIiovcnt\fR argument is valid if greater
than 0 and less than or equal to {\fBIOV_MAX\fR}.
.sp
.LP
The \fBiovec\fR structure contains the following members:
.sp
.in +2
.nf
caddr_t   iov_base;
int       iov_len;
.fi
.in -2

.sp
.LP
Each \fBiovec\fR entry specifies the base address and length of an area in
memory where data should be placed.  The \fBreadv()\fR function always fills an
area completely before proceeding to the next.
.sp
.LP
Upon successful completion, \fBreadv()\fR marks for update the \fBst_atime\fR
field of the file.
.SS "\fBpread()\fR"
.LP
The \fBpread()\fR function performs the same action as \fBread()\fR, except
that it reads from a given position in the file without changing the file
pointer. The first three arguments to \fBpread()\fR are the same as
\fBread()\fR with the addition of a fourth argument \fIoffset\fR for the
desired position inside the file. \fBpread()\fR will read up to the maximum
offset value that can be represented in an \fBoff_t\fR for regular files. An
attempt to perform a \fBpread()\fR on a file that is incapable of seeking
results in an error.
.SS "\fBpreadv()\fR"
.LP
The \fBpreadv()\fR function performs the same action as \fBreadv()\fR except it
reads from a given position in the file without changing the file pointer. The
first three arguments are the same as \fBreadv()\fR with the addition of a
fourth argument \fIoffset\fR for the desired position inside the file.
\fBpreadv()\fR will read up to the maximum offset value that can be represented
in an \fBoff_t\fR for regular files. An attempt to perform a \fBpreadv()\fR on a
file that is incapable of seeking results in an error.

.SH RETURN VALUES
.LP
Upon successful completion, \fBread()\fR and \fBreadv()\fR return a
non-negative integer indicating the number of bytes actually read. Otherwise,
the functions return \fB\(mi1\fR and set \fBerrno\fR to indicate the error.
.SH ERRORS
.LP
In addition to the errors documented below, if the \fIfiledes\fR argument refers
to a socket, then an asynchronous error generated by the underlying socket
protocol may be returned. For the full list of errors, please see the
corresponding socket protocol manual page. For example, for a list of TCP
errors, please see \fBtcp\fR(4P).
.sp
.LP
The \fBread()\fR, \fBreadv()\fR, \fBpread()\fR, and \fBpreadv()\fR functions will fail if:
.sp
.ne 2
.na
\fB\fBEAGAIN\fR\fR
.ad
.RS 11n
Mandatory file/record locking was set, \fBO_NDELAY\fR or \fBO_NONBLOCK\fR was
set, and there was a blocking record lock; total amount of system memory
available when reading using raw I/O is temporarily insufficient; no data is
waiting to be read on a file associated with a tty device and \fBO_NONBLOCK\fR
was set; or no message is waiting to be read on a stream and \fBO_NDELAY\fR or
\fBO_NONBLOCK\fR was set.
.RE

.sp
.ne 2
.na
\fB\fBEBADF\fR\fR
.ad
.RS 11n
The \fIfildes\fR argument is not a valid file descriptor open for reading.
.RE

.sp
.ne 2
.na
\fB\fBEBADMSG\fR\fR
.ad
.RS 11n
Message waiting to be read on a stream is not a data message.
.RE

.sp
.ne 2
.na
\fB\fBECONNRESET\fR\fR
.ad
.RS 11n
The \fIfiledes\fR argument refers to a connection oriented socket and the
connection was forcibly closed by the peer and is no longer valid.  I/O can no
longer be performed to \fIfiledes\fR.
.RE

.sp
.ne 2
.na
\fB\fBEDEADLK\fR\fR
.ad
.RS 11n
The read was going to go to sleep and cause a deadlock to occur.
.RE

.sp
.ne 2
.na
\fB\fBEINTR\fR\fR
.ad
.RS 11n
A signal was caught during the read operation and no data was transferred.
.RE

.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 11n
An attempt was made to read from a stream linked to a multiplexor.
.RE

.sp
.ne 2
.na
\fB\fBEIO\fR\fR
.ad
.RS 11n
A physical I/O error has occurred, or the process is in a background process
group and is attempting to read from its controlling terminal, and either the
process is ignoring or blocking the \fBSIGTTIN\fR signal or the process group
of the process is orphaned.
.RE

.sp
.ne 2
.na
\fB\fBEISDIR\fR\fR
.ad
.RS 11n
The \fIfildes\fR argument refers to a directory on a file system type that does
not support read operations on directories.
.RE

.sp
.ne 2
.na
\fB\fBENOLCK\fR\fR
.ad
.RS 11n
The system record lock table was full, so the \fBread()\fR or \fBreadv()\fR
could not go to sleep until the blocking record lock was removed.
.RE

.sp
.ne 2
.na
\fB\fBENOLINK\fR\fR
.ad
.RS 11n
The \fIfildes\fR argument is on a remote machine and the link to that machine
is no longer active.
.RE

.sp
.ne 2
.na
\fB\fBENXIO\fR\fR
.ad
.RS 11n
The device associated with \fIfildes\fR is a block special or character special
file and the value of the file pointer is out of range.
.RE

.sp
.LP
The \fBread()\fR and \fBpread()\fR functions will fail if:
.sp
.ne 2
.na
\fB\fBEFAULT\fR\fR
.ad
.RS 10n
The \fIbuf\fR argument points to an illegal address.
.RE

.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 10n
The \fInbyte\fR argument overflowed an \fBssize_t\fR.
.RE

.sp
.LP
The \fBread()\fR and \fBreadv()\fR functions will fail if:
.sp
.ne 2
.na
\fB\fBEOVERFLOW\fR\fR
.ad
.RS 13n
The file is a regular file, \fInbyte\fR is greater than 0, the starting
position is before the end-of-file, and the starting position is greater than
or equal to the offset maximum established in the open file description
associated with \fIfildes\fR.
.RE

.sp
.LP
The \fBreadv()\fR and \fBpreadv()\fR functions may fail if:
.sp
.ne 2
.na
\fB\fBEFAULT\fR\fR
.ad
.RS 10n
The \fIiov\fR argument points outside the allocated address space.
.RE

.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 10n
The \fIiovcnt\fR argument was less than or equal to \fB0\fR or greater than
{\fBIOV_MAX\fR}. See \fBIntro\fR(2) for a definition of {\fBIOV_MAX\fR}).
.sp
One of the \fBiov_len\fR values in the \fIiov\fR array was negative, or the sum
of the \fBiov_len\fR values in the \fIiov\fR array overflowed an \fBssize_t\fR.
.RE

.sp
.LP
The \fBpread()\fR and \fBpreadv()\fR functions will fail and the file pointer
remain unchanged if:
.sp
.ne 2
.na
\fB\fBESPIPE\fR\fR
.ad
.RS 10n
The \fIfildes\fR argument is associated with a pipe or FIFO.
.RE

.SH USAGE
.LP
The \fBpread()\fR function has a transitional interface for 64-bit file
offsets.  See \fBlf64\fR(7).
.SH ATTRIBUTES
.LP
See \fBattributes\fR(7) for descriptions of the following attributes:
.sp

.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE	ATTRIBUTE VALUE
_
Interface Stability	Committed
_
MT-Level	\fBread()\fR is Async-Signal-Safe
_
Standard	See \fBstandards\fR(7).
.TE

.SH SEE ALSO
.LP
.BR Intro (2),
.BR chmod (2),
.BR creat (2),
.BR dup (2),
.BR fcntl (2),
.BR getmsg (2),
.BR ioctl (2),
.BR lseek (2),
.BR open (2),
.BR pipe (2),
.BR recv (3SOCKET),
.BR streamio (4I),
.BR termio (4I),
.BR tcp (4P),
.BR attributes (7),
.BR lf64 (7),
.BR standards (7)