1994-05-27 05:00:24 +00:00
|
|
|
.\" Copyright (c) 1980, 1991, 1993
|
|
|
|
.\" The Regents of the University of California. All rights reserved.
|
|
|
|
.\"
|
|
|
|
.\" Redistribution and use in source and binary forms, with or without
|
|
|
|
.\" modification, are permitted provided that the following conditions
|
|
|
|
.\" are met:
|
|
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer.
|
|
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer in the
|
|
|
|
.\" documentation and/or other materials provided with the distribution.
|
2017-02-28 23:42:47 +00:00
|
|
|
.\" 3. Neither the name of the University nor the names of its contributors
|
1994-05-27 05:00:24 +00:00
|
|
|
.\" may be used to endorse or promote products derived from this software
|
|
|
|
.\" without specific prior written permission.
|
|
|
|
.\"
|
|
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
.\" SUCH DAMAGE.
|
|
|
|
.\"
|
|
|
|
.\" @(#)read.2 8.4 (Berkeley) 2/26/94
|
1999-08-28 00:22:10 +00:00
|
|
|
.\" $FreeBSD$
|
1994-05-27 05:00:24 +00:00
|
|
|
.\"
|
2017-12-01 22:26:36 +00:00
|
|
|
.Dd December 1, 2017
|
1994-05-27 05:00:24 +00:00
|
|
|
.Dt READ 2
|
2001-07-10 13:41:46 +00:00
|
|
|
.Os
|
1994-05-27 05:00:24 +00:00
|
|
|
.Sh NAME
|
|
|
|
.Nm read ,
|
1999-04-11 21:14:40 +00:00
|
|
|
.Nm readv ,
|
2005-07-07 18:17:55 +00:00
|
|
|
.Nm pread ,
|
|
|
|
.Nm preadv
|
1994-05-27 05:00:24 +00:00
|
|
|
.Nd read input
|
2000-04-21 09:42:15 +00:00
|
|
|
.Sh LIBRARY
|
|
|
|
.Lb libc
|
1994-05-27 05:00:24 +00:00
|
|
|
.Sh SYNOPSIS
|
2001-10-01 16:09:29 +00:00
|
|
|
.In unistd.h
|
1994-05-27 05:00:24 +00:00
|
|
|
.Ft ssize_t
|
2013-09-12 00:53:38 +00:00
|
|
|
.Fn read "int fd" "void *buf" "size_t nbytes"
|
1994-05-27 05:00:24 +00:00
|
|
|
.Ft ssize_t
|
2013-09-12 00:53:38 +00:00
|
|
|
.Fn pread "int fd" "void *buf" "size_t nbytes" "off_t offset"
|
2012-01-22 11:15:48 +00:00
|
|
|
.In sys/uio.h
|
2005-07-07 18:17:55 +00:00
|
|
|
.Ft ssize_t
|
2013-09-12 00:53:38 +00:00
|
|
|
.Fn readv "int fd" "const struct iovec *iov" "int iovcnt"
|
1999-04-11 21:14:40 +00:00
|
|
|
.Ft ssize_t
|
2013-09-12 00:53:38 +00:00
|
|
|
.Fn preadv "int fd" "const struct iovec *iov" "int iovcnt" "off_t offset"
|
1994-05-27 05:00:24 +00:00
|
|
|
.Sh DESCRIPTION
|
2002-12-18 09:22:32 +00:00
|
|
|
The
|
|
|
|
.Fn read
|
|
|
|
system call
|
1994-05-27 05:00:24 +00:00
|
|
|
attempts to read
|
|
|
|
.Fa nbytes
|
|
|
|
of data from the object referenced by the descriptor
|
2013-09-12 00:53:38 +00:00
|
|
|
.Fa fd
|
1994-05-27 05:00:24 +00:00
|
|
|
into the buffer pointed to by
|
|
|
|
.Fa buf .
|
2002-12-18 09:22:32 +00:00
|
|
|
The
|
|
|
|
.Fn readv
|
|
|
|
system call
|
1994-05-27 05:00:24 +00:00
|
|
|
performs the same action, but scatters the input data
|
2001-07-15 07:53:42 +00:00
|
|
|
into the
|
1994-05-27 05:00:24 +00:00
|
|
|
.Fa iovcnt
|
|
|
|
buffers specified by the members of the
|
|
|
|
.Fa iov
|
|
|
|
array: iov[0], iov[1], ..., iov[iovcnt\|\-\|1].
|
2002-12-18 09:22:32 +00:00
|
|
|
The
|
|
|
|
.Fn pread
|
2005-07-07 18:17:55 +00:00
|
|
|
and
|
|
|
|
.Fn preadv
|
|
|
|
system calls
|
|
|
|
perform the same functions, but read from the specified position in
|
1999-04-11 21:14:40 +00:00
|
|
|
the file without modifying the file pointer.
|
1994-05-27 05:00:24 +00:00
|
|
|
.Pp
|
2001-07-15 07:53:42 +00:00
|
|
|
For
|
2005-07-07 18:17:55 +00:00
|
|
|
.Fn readv
|
|
|
|
and
|
|
|
|
.Fn preadv ,
|
2001-07-15 07:53:42 +00:00
|
|
|
the
|
1994-05-27 05:00:24 +00:00
|
|
|
.Fa iovec
|
|
|
|
structure is defined as:
|
|
|
|
.Pp
|
|
|
|
.Bd -literal -offset indent -compact
|
|
|
|
struct iovec {
|
2003-01-12 15:18:47 +00:00
|
|
|
void *iov_base; /* Base address. */
|
1997-01-30 22:39:40 +00:00
|
|
|
size_t iov_len; /* Length. */
|
1994-05-27 05:00:24 +00:00
|
|
|
};
|
|
|
|
.Ed
|
|
|
|
.Pp
|
2001-07-15 07:53:42 +00:00
|
|
|
Each
|
1994-05-27 05:00:24 +00:00
|
|
|
.Fa iovec
|
|
|
|
entry specifies the base address and length of an area
|
2001-07-15 07:53:42 +00:00
|
|
|
in memory where data should be placed.
|
2002-12-18 09:22:32 +00:00
|
|
|
The
|
|
|
|
.Fn readv
|
|
|
|
system call
|
1994-05-27 05:00:24 +00:00
|
|
|
will always fill an area completely before proceeding
|
|
|
|
to the next.
|
|
|
|
.Pp
|
|
|
|
On objects capable of seeking, the
|
|
|
|
.Fn read
|
|
|
|
starts at a position
|
|
|
|
given by the pointer associated with
|
2013-09-12 00:53:38 +00:00
|
|
|
.Fa fd
|
1994-05-27 05:00:24 +00:00
|
|
|
(see
|
|
|
|
.Xr lseek 2 ) .
|
|
|
|
Upon return from
|
|
|
|
.Fn read ,
|
|
|
|
the pointer is incremented by the number of bytes actually read.
|
|
|
|
.Pp
|
|
|
|
Objects that are not capable of seeking always read from the current
|
2004-07-02 23:52:20 +00:00
|
|
|
position.
|
|
|
|
The value of the pointer associated with such an
|
1994-05-27 05:00:24 +00:00
|
|
|
object is undefined.
|
|
|
|
.Pp
|
|
|
|
Upon successful completion,
|
1999-04-11 21:14:40 +00:00
|
|
|
.Fn read ,
|
|
|
|
.Fn readv ,
|
|
|
|
.Fn pread
|
2005-07-07 18:17:55 +00:00
|
|
|
and
|
|
|
|
.Fn preadv
|
1994-05-27 05:00:24 +00:00
|
|
|
return the number of bytes actually read and placed in the buffer.
|
|
|
|
The system guarantees to read the number of bytes requested if
|
|
|
|
the descriptor references a normal file that has that many bytes left
|
|
|
|
before the end-of-file, but in no other case.
|
|
|
|
.Sh RETURN VALUES
|
|
|
|
If successful, the
|
2000-03-02 09:14:21 +00:00
|
|
|
number of bytes actually read is returned.
|
|
|
|
Upon reading end-of-file,
|
1994-05-27 05:00:24 +00:00
|
|
|
zero is returned.
|
|
|
|
Otherwise, a -1 is returned and the global variable
|
|
|
|
.Va errno
|
|
|
|
is set to indicate the error.
|
|
|
|
.Sh ERRORS
|
2002-12-18 09:22:32 +00:00
|
|
|
The
|
|
|
|
.Fn read ,
|
1999-04-11 21:14:40 +00:00
|
|
|
.Fn readv ,
|
|
|
|
.Fn pread
|
2005-07-07 18:17:55 +00:00
|
|
|
and
|
|
|
|
.Fn preadv
|
2002-12-18 09:22:32 +00:00
|
|
|
system calls
|
1994-05-27 05:00:24 +00:00
|
|
|
will succeed unless:
|
|
|
|
.Bl -tag -width Er
|
|
|
|
.It Bq Er EBADF
|
2002-12-19 09:40:28 +00:00
|
|
|
The
|
2013-09-12 00:53:38 +00:00
|
|
|
.Fa fd
|
2002-12-19 09:40:28 +00:00
|
|
|
argument
|
1994-05-27 05:00:24 +00:00
|
|
|
is not a valid file or socket descriptor open for reading.
|
2005-07-29 07:42:10 +00:00
|
|
|
.It Bq Er ECONNRESET
|
|
|
|
The
|
2013-09-12 00:53:38 +00:00
|
|
|
.Fa fd
|
2005-07-29 07:42:10 +00:00
|
|
|
argument refers to a socket, and the remote socket end is
|
|
|
|
forcibly closed.
|
1994-05-27 05:00:24 +00:00
|
|
|
.It Bq Er EFAULT
|
2002-12-19 09:40:28 +00:00
|
|
|
The
|
|
|
|
.Fa buf
|
|
|
|
argument
|
1994-05-27 05:00:24 +00:00
|
|
|
points outside the allocated address space.
|
|
|
|
.It Bq Er EIO
|
|
|
|
An I/O error occurred while reading from the file system.
|
2012-04-29 22:23:00 +00:00
|
|
|
.It Bq Er EBUSY
|
|
|
|
Failed to read from a file, e.g. /proc/<pid>/regs while <pid> is not stopped
|
1994-05-27 05:00:24 +00:00
|
|
|
.It Bq Er EINTR
|
2006-10-11 13:33:02 +00:00
|
|
|
A read from a slow device
|
2006-10-21 18:03:53 +00:00
|
|
|
(i.e.\& one that might block for an arbitrary amount of time)
|
2006-10-11 13:33:02 +00:00
|
|
|
was interrupted by the delivery of a signal
|
|
|
|
before any data arrived.
|
1994-05-27 05:00:24 +00:00
|
|
|
.It Bq Er EINVAL
|
|
|
|
The pointer associated with
|
2013-09-12 00:53:38 +00:00
|
|
|
.Fa fd
|
1994-05-27 05:00:24 +00:00
|
|
|
was negative.
|
|
|
|
.It Bq Er EAGAIN
|
|
|
|
The file was marked for non-blocking I/O,
|
|
|
|
and no data were ready to be read.
|
2003-09-02 16:50:17 +00:00
|
|
|
.It Bq Er EISDIR
|
|
|
|
The file descriptor is associated with a directory residing
|
2004-06-30 20:09:10 +00:00
|
|
|
on a file system that does not allow regular read operations on
|
2004-07-02 23:52:20 +00:00
|
|
|
directories (e.g.\& NFS).
|
2003-09-02 16:50:17 +00:00
|
|
|
.It Bq Er EOPNOTSUPP
|
2004-06-30 20:09:10 +00:00
|
|
|
The file descriptor is associated with a file system and file type that
|
2003-09-02 16:50:17 +00:00
|
|
|
do not allow regular read operations on it.
|
2004-01-25 06:24:08 +00:00
|
|
|
.It Bq Er EOVERFLOW
|
|
|
|
The file descriptor is associated with a regular file,
|
|
|
|
.Fa nbytes
|
|
|
|
is greater than 0,
|
|
|
|
.Fa offset
|
|
|
|
is before the end-of-file, and
|
|
|
|
.Fa offset
|
|
|
|
is greater than or equal to the offset maximum established
|
2004-06-30 20:09:10 +00:00
|
|
|
for this file system.
|
2005-02-10 20:09:01 +00:00
|
|
|
.It Bq Er EINVAL
|
|
|
|
The value
|
|
|
|
.Fa nbytes
|
|
|
|
is greater than
|
|
|
|
.Dv INT_MAX .
|
1994-05-27 05:00:24 +00:00
|
|
|
.El
|
|
|
|
.Pp
|
2001-07-15 07:53:42 +00:00
|
|
|
In addition,
|
1994-05-27 05:00:24 +00:00
|
|
|
.Fn readv
|
2005-07-07 18:17:55 +00:00
|
|
|
and
|
|
|
|
.Fn preadv
|
1994-05-27 05:00:24 +00:00
|
|
|
may return one of the following errors:
|
|
|
|
.Bl -tag -width Er
|
|
|
|
.It Bq Er EINVAL
|
2002-12-19 09:40:28 +00:00
|
|
|
The
|
|
|
|
.Fa iovcnt
|
|
|
|
argument
|
2004-06-15 01:24:41 +00:00
|
|
|
was less than or equal to 0, or greater than
|
2004-06-30 20:09:10 +00:00
|
|
|
.Dv IOV_MAX .
|
1994-05-27 05:00:24 +00:00
|
|
|
.It Bq Er EINVAL
|
|
|
|
One of the
|
|
|
|
.Fa iov_len
|
|
|
|
values in the
|
|
|
|
.Fa iov
|
|
|
|
array was negative.
|
|
|
|
.It Bq Er EINVAL
|
|
|
|
The sum of the
|
|
|
|
.Fa iov_len
|
|
|
|
values in the
|
|
|
|
.Fa iov
|
|
|
|
array overflowed a 32-bit integer.
|
|
|
|
.It Bq Er EFAULT
|
|
|
|
Part of the
|
|
|
|
.Fa iov
|
2004-11-15 13:55:33 +00:00
|
|
|
array points outside the process's allocated address space.
|
1994-05-27 05:00:24 +00:00
|
|
|
.El
|
1999-04-11 21:14:40 +00:00
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn pread
|
2005-07-07 18:17:55 +00:00
|
|
|
and
|
|
|
|
.Fn preadv
|
|
|
|
system calls may also return the following errors:
|
1999-04-11 21:14:40 +00:00
|
|
|
.Bl -tag -width Er
|
|
|
|
.It Bq Er EINVAL
|
2004-10-25 13:35:03 +00:00
|
|
|
The
|
|
|
|
.Fa offset
|
|
|
|
value was negative.
|
1999-04-11 21:14:40 +00:00
|
|
|
.It Bq Er ESPIPE
|
|
|
|
The file descriptor is associated with a pipe, socket, or FIFO.
|
|
|
|
.El
|
1994-05-27 05:00:24 +00:00
|
|
|
.Sh SEE ALSO
|
|
|
|
.Xr dup 2 ,
|
|
|
|
.Xr fcntl 2 ,
|
2003-09-02 16:50:17 +00:00
|
|
|
.Xr getdirentries 2 ,
|
1994-05-27 05:00:24 +00:00
|
|
|
.Xr open 2 ,
|
|
|
|
.Xr pipe 2 ,
|
|
|
|
.Xr select 2 ,
|
|
|
|
.Xr socket 2 ,
|
2003-09-02 16:50:17 +00:00
|
|
|
.Xr socketpair 2 ,
|
|
|
|
.Xr fread 3 ,
|
|
|
|
.Xr readdir 3
|
1994-05-27 05:00:24 +00:00
|
|
|
.Sh STANDARDS
|
1996-12-02 20:03:58 +00:00
|
|
|
The
|
|
|
|
.Fn read
|
2002-12-18 09:22:32 +00:00
|
|
|
system call is expected to conform to
|
1996-12-02 20:03:58 +00:00
|
|
|
.St -p1003.1-90 .
|
1999-04-11 21:14:40 +00:00
|
|
|
The
|
|
|
|
.Fn readv
|
|
|
|
and
|
|
|
|
.Fn pread
|
2002-12-18 09:22:32 +00:00
|
|
|
system calls are expected to conform to
|
1999-04-11 21:14:40 +00:00
|
|
|
.St -xpg4.2 .
|
1994-05-27 05:00:24 +00:00
|
|
|
.Sh HISTORY
|
|
|
|
The
|
2005-07-07 18:17:55 +00:00
|
|
|
.Fn preadv
|
|
|
|
system call appeared in
|
|
|
|
.Fx 6.0 .
|
|
|
|
The
|
1999-04-11 21:14:40 +00:00
|
|
|
.Fn pread
|
2002-12-18 09:22:32 +00:00
|
|
|
function appeared in
|
1999-04-11 21:14:40 +00:00
|
|
|
.At V.4 .
|
|
|
|
The
|
1994-05-27 05:00:24 +00:00
|
|
|
.Fn readv
|
2002-12-18 09:22:32 +00:00
|
|
|
system call appeared in
|
1994-05-27 05:00:24 +00:00
|
|
|
.Bx 4.2 .
|
2002-12-18 09:22:32 +00:00
|
|
|
The
|
1996-08-22 23:31:07 +00:00
|
|
|
.Fn read
|
2002-12-18 09:22:32 +00:00
|
|
|
function appeared in
|
2017-12-01 22:26:36 +00:00
|
|
|
.At v1 .
|