Blame man2/readv.2

Packit 7cfc04
.\" Copyright (C) 2007, 2010 Michael Kerrisk <mtk.manpages@gmail.com>
Packit 7cfc04
.\" and Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(VERBATIM)
Packit 7cfc04
.\" Permission is granted to make and distribute verbatim copies of this
Packit 7cfc04
.\" manual provided the copyright notice and this permission notice are
Packit 7cfc04
.\" preserved on all copies.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Permission is granted to copy and distribute modified versions of this
Packit 7cfc04
.\" manual under the conditions for verbatim copying, provided that the
Packit 7cfc04
.\" entire resulting derived work is distributed under the terms of a
Packit 7cfc04
.\" permission notice identical to this one.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Since the Linux kernel and libraries are constantly changing, this
Packit 7cfc04
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
Packit 7cfc04
.\" responsibility for errors or omissions, or for damages resulting from
Packit 7cfc04
.\" the use of the information contained herein.  The author(s) may not
Packit 7cfc04
.\" have taken the same level of care in the production of this manual,
Packit 7cfc04
.\" which is licensed free of charge, as they might when working
Packit 7cfc04
.\" professionally.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Formatted or processed versions of this manual, if unaccompanied by
Packit 7cfc04
.\" the source, must acknowledge the copyright and authors of this work.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.\" Modified Sat Jul 24 18:34:44 1993 by Rik Faith (faith@cs.unc.edu)
Packit 7cfc04
.\" Merged readv.[23], 2002-10-17, aeb
Packit 7cfc04
.\" 2007-04-30 mtk, A fairly major rewrite to fix errors and
Packit 7cfc04
.\"     add more details.
Packit 7cfc04
.\" 2010-11-16, mtk, Added documentation of preadv() and pwritev()
Packit 7cfc04
.\"
Packit 7cfc04
.TH READV 2  2017-09-15 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
readv, writev, preadv, pwritev, preadv2, pwritev2 \- read or write data into multiple buffers
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.nf
Packit 7cfc04
.B #include <sys/uio.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "ssize_t readv(int " fd ", const struct iovec *" iov ", int " iovcnt );
Packit 7cfc04
.PP
Packit 7cfc04
.BI "ssize_t writev(int " fd ", const struct iovec *" iov ", int " iovcnt );
Packit 7cfc04
.PP
Packit 7cfc04
.BI "ssize_t preadv(int " fd ", const struct iovec *" iov ", int " iovcnt ,
Packit 7cfc04
.BI "               off_t " offset );
Packit 7cfc04
.PP
Packit 7cfc04
.BI "ssize_t pwritev(int " fd ", const struct iovec *" iov ", int " iovcnt ,
Packit 7cfc04
.BI "                off_t " offset );
Packit 7cfc04
.PP
Packit 7cfc04
.BI "ssize_t preadv2(int " fd ", const struct iovec *" iov ", int " iovcnt ,
Packit 7cfc04
.BI "                off_t " offset ", int " flags );
Packit 7cfc04
.PP
Packit 7cfc04
.BI "ssize_t pwritev2(int " fd ", const struct iovec *" iov ", int " iovcnt ,
Packit 7cfc04
.BI "                 off_t " offset ", int " flags );
Packit 7cfc04
.fi
Packit 7cfc04
.PP
Packit 7cfc04
.in -4n
Packit 7cfc04
Feature Test Macro Requirements for glibc (see
Packit 7cfc04
.BR feature_test_macros (7)):
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
.BR preadv (),
Packit 7cfc04
.BR pwritev ():
Packit 7cfc04
    Since glibc 2.19:
Packit 7cfc04
        _DEFAULT_SOURCE
Packit 7cfc04
    Glibc 2.19 and earlier:
Packit 7cfc04
        _BSD_SOURCE
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
The
Packit 7cfc04
.BR readv ()
Packit 7cfc04
system call reads
Packit 7cfc04
.I iovcnt
Packit 7cfc04
buffers from the file associated with the file descriptor
Packit 7cfc04
.I fd
Packit 7cfc04
into the buffers described by
Packit 7cfc04
.I iov
Packit 7cfc04
("scatter input").
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.BR writev ()
Packit 7cfc04
system call writes
Packit 7cfc04
.I iovcnt
Packit 7cfc04
buffers of data described by
Packit 7cfc04
.I iov
Packit 7cfc04
to the file associated with the file descriptor
Packit 7cfc04
.I fd
Packit 7cfc04
("gather output").
Packit 7cfc04
.PP
Packit 7cfc04
The pointer
Packit 7cfc04
.I iov
Packit 7cfc04
points to an array of
Packit 7cfc04
.I iovec
Packit 7cfc04
structures,
Packit 7cfc04
defined in
Packit 7cfc04
.I <sys/uio.h>
Packit 7cfc04
as:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
struct iovec {
Packit 7cfc04
    void  *iov_base;    /* Starting address */
Packit 7cfc04
    size_t iov_len;     /* Number of bytes to transfer */
Packit 7cfc04
};
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.BR readv ()
Packit 7cfc04
system call works just like
Packit 7cfc04
.BR read (2)
Packit 7cfc04
except that multiple buffers are filled.
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.BR writev ()
Packit 7cfc04
system call works just like
Packit 7cfc04
.BR write (2)
Packit 7cfc04
except that multiple buffers are written out.
Packit 7cfc04
.PP
Packit 7cfc04
Buffers are processed in array order.
Packit 7cfc04
This means that
Packit 7cfc04
.BR readv ()
Packit 7cfc04
completely fills
Packit 7cfc04
.IR iov [0]
Packit 7cfc04
before proceeding to
Packit 7cfc04
.IR iov [1],
Packit 7cfc04
and so on.
Packit 7cfc04
(If there is insufficient data, then not all buffers pointed to by
Packit 7cfc04
.I iov
Packit 7cfc04
may be filled.)
Packit 7cfc04
Similarly,
Packit 7cfc04
.BR writev ()
Packit 7cfc04
writes out the entire contents of
Packit 7cfc04
.IR iov [0]
Packit 7cfc04
before proceeding to
Packit 7cfc04
.IR iov [1],
Packit 7cfc04
and so on.
Packit 7cfc04
.PP
Packit 7cfc04
The data transfers performed by
Packit 7cfc04
.BR readv ()
Packit 7cfc04
and
Packit 7cfc04
.BR writev ()
Packit 7cfc04
are atomic: the data written by
Packit 7cfc04
.\" Regarding atomicity, see https://bugzilla.kernel.org/show_bug.cgi?id=10596
Packit 7cfc04
.BR writev ()
Packit 7cfc04
is written as a single block that is not intermingled with output
Packit 7cfc04
from writes in other processes (but see
Packit 7cfc04
.BR pipe (7)
Packit 7cfc04
for an exception);
Packit 7cfc04
analogously,
Packit 7cfc04
.BR readv ()
Packit 7cfc04
is guaranteed to read a contiguous block of data from the file,
Packit 7cfc04
regardless of read operations performed in other threads or processes
Packit 7cfc04
that have file descriptors referring to the same open file description
Packit 7cfc04
(see
Packit 7cfc04
.BR open (2)).
Packit 7cfc04
.SS preadv() and pwritev()
Packit 7cfc04
The
Packit 7cfc04
.BR preadv ()
Packit 7cfc04
system call combines the functionality of
Packit 7cfc04
.BR readv ()
Packit 7cfc04
and
Packit 7cfc04
.BR pread (2).
Packit 7cfc04
It performs the same task as
Packit 7cfc04
.BR readv (),
Packit 7cfc04
but adds a fourth argument,
Packit 7cfc04
.IR offset ,
Packit 7cfc04
which specifies the file offset at which the input operation
Packit 7cfc04
is to be performed.
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.BR pwritev ()
Packit 7cfc04
system call combines the functionality of
Packit 7cfc04
.BR writev ()
Packit 7cfc04
and
Packit 7cfc04
.BR pwrite (2).
Packit 7cfc04
It performs the same task as
Packit 7cfc04
.BR writev (),
Packit 7cfc04
but adds a fourth argument,
Packit 7cfc04
.IR offset ,
Packit 7cfc04
which specifies the file offset at which the output operation
Packit 7cfc04
is to be performed.
Packit 7cfc04
.PP
Packit 7cfc04
The file offset is not changed by these system calls.
Packit 7cfc04
The file referred to by
Packit 7cfc04
.I fd
Packit 7cfc04
must be capable of seeking.
Packit 7cfc04
.SS preadv2() and pwritev2()
Packit 7cfc04
.PP
Packit 7cfc04
These system calls are similar to
Packit 7cfc04
.BR preadv ()
Packit 7cfc04
and
Packit 7cfc04
.BR pwritev ()
Packit 7cfc04
calls, but add a fifth argument,
Packit 7cfc04
.IR flags ,
Packit 7cfc04
which modifies the behavior on a per-call basis.
Packit 7cfc04
.PP
Packit 7cfc04
Unlike
Packit 7cfc04
.BR preadv ()
Packit 7cfc04
and
Packit 7cfc04
.BR pwritev (),
Packit 7cfc04
if the
Packit 7cfc04
.I offset
Packit 7cfc04
argument is \-1, then the current file offset is used and updated.
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.I flags
Packit 7cfc04
argument contains a bitwise OR of zero or more of the following flags:
Packit 7cfc04
.TP
Packit 7cfc04
.BR RWF_DSYNC " (since Linux 4.7)"
Packit 7cfc04
.\" commit e864f39569f4092c2b2bc72c773b6e486c7e3bd9
Packit 7cfc04
Provide a per-write equivalent of the
Packit 7cfc04
.B O_DSYNC
Packit 7cfc04
.BR open (2)
Packit 7cfc04
flag.
Packit 7cfc04
This flag is meaningful only for
Packit 7cfc04
.BR pwritev2 (),
Packit 7cfc04
and its effect applies only to the data range written by the system call.
Packit 7cfc04
.TP
Packit 7cfc04
.BR RWF_HIPRI " (since Linux 4.6)"
Packit 7cfc04
High priority read/write.
Packit 7cfc04
Allows block-based filesystems to use polling of the device,
Packit 7cfc04
which provides lower latency, but may use additional resources.
Packit 7cfc04
(Currently, this feature is usable only on a file descriptor opened using the
Packit 7cfc04
.BR O_DIRECT
Packit 7cfc04
flag.)
Packit 7cfc04
.TP
Packit 7cfc04
.BR RWF_SYNC " (since Linux 4.7)"
Packit 7cfc04
.\" commit e864f39569f4092c2b2bc72c773b6e486c7e3bd9
Packit 7cfc04
Provide a per-write equivalent of the
Packit 7cfc04
.B O_SYNC
Packit 7cfc04
.BR open (2)
Packit 7cfc04
flag.
Packit 7cfc04
This flag is meaningful only for
Packit 7cfc04
.BR pwritev2 (),
Packit 7cfc04
and its effect applies only to the data range written by the system call.
Packit 7cfc04
.TP
Packit 7cfc04
.BR RWF_NOWAIT " (since Linux 4.14)"
Packit 7cfc04
.\" commit 3239d834847627b6634a4139cf1dc58f6f137a46
Packit 7cfc04
.\" commit 91f9943e1c7b6638f27312d03fe71fcc67b23571
Packit 7cfc04
Do not wait for data which is not immediately available.
Packit 7cfc04
If this flag is specified, the
Packit 7cfc04
.BR preadv2 ()
Packit 7cfc04
system call will return instantly if it would have to read data from
Packit 7cfc04
the backing storage or wait for a lock.
Packit 7cfc04
If some data was successfully read, it will return the number of bytes read.
Packit 7cfc04
If no bytes were read, it will return -1 and set
Packit 7cfc04
.IR errno
Packit 7cfc04
to
Packit 7cfc04
.BR EAGAIN .
Packit 7cfc04
Currently, this flag is meaningful only for
Packit 7cfc04
.BR preadv2 ().
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
On success,
Packit 7cfc04
.BR readv (),
Packit 7cfc04
.BR preadv ()
Packit 7cfc04
and
Packit 7cfc04
.BR preadv2 ()
Packit 7cfc04
return the number of bytes read;
Packit 7cfc04
.BR writev (),
Packit 7cfc04
.BR pwritev ()
Packit 7cfc04
and
Packit 7cfc04
.BR pwritev2 ()
Packit 7cfc04
return the number of bytes written.
Packit 7cfc04
.PP
Packit 7cfc04
Note that it is not an error for a successful call to transfer fewer bytes
Packit 7cfc04
than requested (see
Packit 7cfc04
.BR read (2)
Packit 7cfc04
and
Packit 7cfc04
.BR write (2)).
Packit 7cfc04
.PP
Packit 7cfc04
On error, \-1 is returned, and \fIerrno\fP is set appropriately.
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
The errors are as given for
Packit 7cfc04
.BR read (2)
Packit 7cfc04
and
Packit 7cfc04
.BR write (2).
Packit 7cfc04
Furthermore,
Packit 7cfc04
.BR preadv (),
Packit 7cfc04
.BR preadv2 (),
Packit 7cfc04
.BR pwritev (),
Packit 7cfc04
and
Packit 7cfc04
.BR pwritev2 ()
Packit 7cfc04
can also fail for the same reasons as
Packit 7cfc04
.BR lseek (2).
Packit 7cfc04
Additionally, the following errors are defined:
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
The sum of the
Packit 7cfc04
.I iov_len
Packit 7cfc04
values overflows an
Packit 7cfc04
.I ssize_t
Packit 7cfc04
value.
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
The vector count,
Packit 7cfc04
.IR iovcnt ,
Packit 7cfc04
is less than zero or greater than the permitted maximum.
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
An unknown flag is specified in \fIflags\fP.
Packit 7cfc04
.SH VERSIONS
Packit 7cfc04
.BR preadv ()
Packit 7cfc04
and
Packit 7cfc04
.BR pwritev ()
Packit 7cfc04
first appeared in Linux 2.6.30; library support was added in glibc 2.10.
Packit 7cfc04
.PP
Packit 7cfc04
.BR preadv2 ()
Packit 7cfc04
and
Packit 7cfc04
.BR pwritev2 ()
Packit 7cfc04
first appeared in Linux 4.6.
Packit 7cfc04
Library support was added in glibc 2.26.
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
.BR readv (),
Packit 7cfc04
.BR writev ():
Packit 7cfc04
POSIX.1-2001, POSIX.1-2008,
Packit 7cfc04
4.4BSD (these system calls first appeared in 4.2BSD).
Packit 7cfc04
.\" Linux libc5 used \fIsize_t\fP as the type of the \fIiovcnt\fP argument,
Packit 7cfc04
.\" and \fIint\fP as the return type.
Packit 7cfc04
.\" The readv/writev system calls were buggy before Linux 1.3.40.
Packit 7cfc04
.\" (Says release.libc.)
Packit 7cfc04
.PP
Packit 7cfc04
.BR preadv (),
Packit 7cfc04
.BR pwritev ():
Packit 7cfc04
nonstandard, but present also on the modern BSDs.
Packit 7cfc04
.PP
Packit 7cfc04
.BR preadv2 (),
Packit 7cfc04
.BR pwritev2 ():
Packit 7cfc04
nonstandard Linux extension.
Packit 7cfc04
.SH NOTES
Packit 7cfc04
POSIX.1 allows an implementation to place a limit on
Packit 7cfc04
the number of items that can be passed in
Packit 7cfc04
.IR iov .
Packit 7cfc04
An implementation can advertise its limit by defining
Packit 7cfc04
.B IOV_MAX
Packit 7cfc04
in
Packit 7cfc04
.I <limits.h>
Packit 7cfc04
or at run time via the return value from
Packit 7cfc04
.IR sysconf(_SC_IOV_MAX) .
Packit 7cfc04
On modern Linux systems, the limit is 1024.
Packit 7cfc04
Back in Linux 2.0 days, this limit was 16.
Packit 7cfc04
.\"
Packit 7cfc04
.\"
Packit 7cfc04
.SS C library/kernel differences
Packit 7cfc04
The raw
Packit 7cfc04
.BR preadv ()
Packit 7cfc04
and
Packit 7cfc04
.BR pwritev ()
Packit 7cfc04
system calls have call signatures that differ slightly from that of the
Packit 7cfc04
corresponding GNU C library wrapper functions shown in the SYNOPSIS.
Packit 7cfc04
The final argument,
Packit 7cfc04
.IR offset ,
Packit 7cfc04
is unpacked by the wrapper functions into two arguments in the system calls:
Packit 7cfc04
.PP
Packit 7cfc04
.BI "    unsigned long " pos_l ", unsigned long " pos
Packit 7cfc04
.PP
Packit 7cfc04
These arguments contain, respectively, the low order and high order 32 bits of
Packit 7cfc04
.IR offset .
Packit 7cfc04
.SS Historical C library/kernel differences
Packit 7cfc04
To deal with the fact that
Packit 7cfc04
.B IOV_MAX
Packit 7cfc04
was so low on early versions of Linux,
Packit 7cfc04
the glibc wrapper functions for
Packit 7cfc04
.BR readv ()
Packit 7cfc04
and
Packit 7cfc04
.BR writev ()
Packit 7cfc04
did some extra work if they detected that the underlying kernel
Packit 7cfc04
system call failed because this limit was exceeded.
Packit 7cfc04
In the case of
Packit 7cfc04
.BR readv (),
Packit 7cfc04
the wrapper function allocated a temporary buffer large enough
Packit 7cfc04
for all of the items specified by
Packit 7cfc04
.IR iov ,
Packit 7cfc04
passed that buffer in a call to
Packit 7cfc04
.BR read (2),
Packit 7cfc04
copied data from the buffer to the locations specified by the
Packit 7cfc04
.I iov_base
Packit 7cfc04
fields of the elements of
Packit 7cfc04
.IR iov ,
Packit 7cfc04
and then freed the buffer.
Packit 7cfc04
The wrapper function for
Packit 7cfc04
.BR writev ()
Packit 7cfc04
performed the analogous task using a temporary buffer and a call to
Packit 7cfc04
.BR write (2).
Packit 7cfc04
.PP
Packit 7cfc04
The need for this extra effort in the glibc wrapper functions
Packit 7cfc04
went away with Linux 2.2 and later.
Packit 7cfc04
However, glibc continued to provide this behavior until version 2.10.
Packit 7cfc04
Starting with glibc version 2.9,
Packit 7cfc04
the wrapper functions provide this behavior only if the library detects
Packit 7cfc04
that the system is running a Linux kernel older than version 2.6.18
Packit 7cfc04
(an arbitrarily selected kernel version).
Packit 7cfc04
And since glibc 2.20
Packit 7cfc04
(which requires a minimum Linux kernel version of 2.6.32),
Packit 7cfc04
the glibc wrapper functions always just directly invoke the system calls.
Packit 7cfc04
.SH EXAMPLE
Packit 7cfc04
The following code sample demonstrates the use of
Packit 7cfc04
.BR writev ():
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
char *str0 = "hello ";
Packit 7cfc04
char *str1 = "world\\n";
Packit 7cfc04
struct iovec iov[2];
Packit 7cfc04
ssize_t nwritten;
Packit 7cfc04
Packit 7cfc04
iov[0].iov_base = str0;
Packit 7cfc04
iov[0].iov_len = strlen(str0);
Packit 7cfc04
iov[1].iov_base = str1;
Packit 7cfc04
iov[1].iov_len = strlen(str1);
Packit 7cfc04
Packit 7cfc04
nwritten = writev(STDOUT_FILENO, iov, 2);
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR pread (2),
Packit 7cfc04
.BR read (2),
Packit 7cfc04
.BR write (2)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.