man-pages/man2/sendfile.2.html
2021-03-31 01:06:50 +01:00

375 lines
7.3 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of SENDFILE</TITLE>
</HEAD><BODY>
<H1>SENDFILE</H1>
Section: Linux Programmer's Manual (2)<BR>Updated: 2017-09-15<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
sendfile - transfer data between file descriptors
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<B>#include &lt;<A HREF="file:///usr/include/sys/sendfile.h">sys/sendfile.h</A>&gt;</B>
<P>
<B>ssize_t sendfile(int</B><I> out_fd</I><B>, int</B><I> in_fd</I><B>, off_t *</B><I></I><B>offset</B><I>, size_t</I><B> count</B><I>);</I>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
<B>sendfile</B>()
copies data between one file descriptor and another.
Because this copying is done within the kernel,
<B>sendfile</B>()
is more efficient than the combination of
<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2)
and
<B><A HREF="/cgi-bin/man/man2html?2+write">write</A></B>(2),
which would require transferring data to and from user space.
<P>
<I>in_fd</I>
should be a file descriptor opened for reading and
<I>out_fd</I>
should be a descriptor opened for writing.
<P>
If
<I>offset</I>
is not NULL, then it points
to a variable holding the file offset from which
<B>sendfile</B>()
will start reading data from
<I>in_fd</I>.
When
<B>sendfile</B>()
returns, this variable
will be set to the offset of the byte following the last byte that was read.
If
<I>offset</I>
is not NULL, then
<B>sendfile</B>()
does not modify the file offset of
<I>in_fd</I>;
otherwise the file offset is adjusted to reflect
the number of bytes read from
<I>in_fd</I>.
<P>
If
<I>offset</I>
is NULL, then data will be read from
<I>in_fd</I>
starting at the file offset,
and the file offset will be updated by the call.
<P>
<I>count</I>
is the number of bytes to copy between the file descriptors.
<P>
The
<I>in_fd</I>
argument must correspond to a file which supports
<B><A HREF="/cgi-bin/man/man2html?2+mmap">mmap</A></B>(2)-like
operations
(i.e., it cannot be a socket).
<P>
In Linux kernels before 2.6.33,
<I>out_fd</I>
must refer to a socket.
Since Linux 2.6.33 it can be any file.
If it is a regular file, then
<B>sendfile</B>()
changes the file offset appropriately.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
If the transfer was successful, the number of bytes written to
<I>out_fd</I>
is returned.
Note that a successful call to
<B>sendfile</B>()
may write fewer bytes than requested;
the caller should be prepared to retry the call if there were unsent bytes.
See also NOTES.
<P>
On error, -1 is returned, and
<I>errno</I>
is set appropriately.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="1"><B>EAGAIN</B>
<DD>
Nonblocking I/O has been selected using
<B>O_NONBLOCK</B>
and the write would block.
<DT id="2"><B>EBADF</B>
<DD>
The input file was not opened for reading or the output file
was not opened for writing.
<DT id="3"><B>EFAULT</B>
<DD>
Bad address.
<DT id="4"><B>EINVAL</B>
<DD>
Descriptor is not valid or locked, or an
<B><A HREF="/cgi-bin/man/man2html?2+mmap">mmap</A></B>(2)-like
operation is not available for
<I>in_fd</I>,
or
<I>count</I>
is negative.
<DT id="5"><B>EINVAL</B>
<DD>
<I>out_fd</I>
has the
<B>O_APPEND</B>
flag set.
This is not currently supported by
<B>sendfile</B>().
<DT id="6"><B>EIO</B>
<DD>
Unspecified error while reading from
<I>in_fd</I>.
<DT id="7"><B>ENOMEM</B>
<DD>
Insufficient memory to read from
<I>in_fd</I>.
<DT id="8"><B>EOVERFLOW</B>
<DD>
<I>count</I>
is too large, the operation would result in exceeding the maximum size of either
the input file or the output file.
<DT id="9"><B>ESPIPE</B>
<DD>
<I>offset</I>
is not NULL but the input file is not
<B><A HREF="/cgi-bin/man/man2html?2+seek">seek</A></B>(2)-able.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>VERSIONS</H2>
<B>sendfile</B>()
first appeared in Linux 2.2.
The include file
<I>&lt;<A HREF="file:///usr/include/sys/sendfile.h">sys/sendfile.h</A>&gt;</I>
is present since glibc 2.1.
<A NAME="lbAH">&nbsp;</A>
<H2>CONFORMING TO</H2>
Not specified in POSIX.1-2001, nor in other standards.
<P>
Other UNIX systems implement
<B>sendfile</B>()
with different semantics and prototypes.
It should not be used in portable programs.
<A NAME="lbAI">&nbsp;</A>
<H2>NOTES</H2>
<B>sendfile</B>()
will transfer at most 0x7ffff000 (2,147,479,552) bytes,
returning the number of bytes actually transferred.
(This is true on both 32-bit and 64-bit systems.)
<P>
If you plan to use
<B>sendfile</B>()
for sending files to a TCP socket, but need
to send some header data in front of the file contents, you will find
it useful to employ the
<B>TCP_CORK</B>
option, described in
<B><A HREF="/cgi-bin/man/man2html?7+tcp">tcp</A></B>(7),
to minimize the number of packets and to tune performance.
<P>
In Linux 2.4 and earlier,
<I>out_fd</I>
could also refer to a regular file;
this possibility went away in the Linux 2.6.x kernel series,
but was restored in Linux 2.6.33.
<P>
The original Linux
<B>sendfile</B>()
system call was not designed to handle large file offsets.
Consequently, Linux 2.4 added
<B>sendfile64</B>(),
with a wider type for the
<I>offset</I>
argument.
The glibc
<B>sendfile</B>()
wrapper function transparently deals with the kernel differences.
<P>
Applications may wish to fall back to
<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2)/<B><A HREF="/cgi-bin/man/man2html?2+write">write</A></B>(2)
in the case where
<B>sendfile</B>()
fails with
<B>EINVAL</B>
or
<B>ENOSYS</B>.
<P>
If
<I>out_fd</I>
refers to a socket or pipe with zero-copy support, callers must ensure the
transferred portions of the file referred to by
<I>in_fd</I>
remain unmodified until the reader on the other end of
<I>out_fd</I>
has consumed the transferred data.
<P>
The Linux-specific
<B><A HREF="/cgi-bin/man/man2html?2+splice">splice</A></B>(2)
call supports transferring data between arbitrary file descriptors
provided one (or both) of them is a pipe.
<A NAME="lbAJ">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+copy_file_range">copy_file_range</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+mmap">mmap</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+socket">socket</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+splice">splice</A></B>(2)
<A NAME="lbAK">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="10"><A HREF="#lbAB">NAME</A><DD>
<DT id="11"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="12"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="13"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="14"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="15"><A HREF="#lbAG">VERSIONS</A><DD>
<DT id="16"><A HREF="#lbAH">CONFORMING TO</A><DD>
<DT id="17"><A HREF="#lbAI">NOTES</A><DD>
<DT id="18"><A HREF="#lbAJ">SEE ALSO</A><DD>
<DT id="19"><A HREF="#lbAK">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:34 GMT, March 31, 2021
</BODY>
</HTML>