|
NAMEsendfile —
send a file to a socket
LIBRARYStandard C Library (libc, -lc)SYNOPSIS#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
int
DESCRIPTIONThesendfile () system call sends a regular file or
shared memory object specified by descriptor fd out a
stream socket specified by descriptor s.
The offset argument specifies where to begin in the file. Should offset fall beyond the end of file, the system will return success and report 0 bytes sent as described below. The nbytes argument specifies how many bytes of the file should be sent, with 0 having the special meaning of send until the end of file has been reached. An optional header and/or trailer can be sent before and after the file data by specifying a pointer to a struct sf_hdtr, which has the following structure: struct sf_hdtr { struct iovec *headers; /* pointer to header iovecs */ int hdr_cnt; /* number of header iovecs */ struct iovec *trailers; /* pointer to trailer iovecs */ int trl_cnt; /* number of trailer iovecs */ }; The headers and
trailers pointers, if
non- If non- The least significant 16 bits of flags argument is a bitmap of these values:
When using a socket marked for non-blocking I/O,
SETTING READAHEADsendfile uses internal heuristics based on request size
and file system layout to do readahead. Additionally application may request
extra readahead. The most significant 16 bits of flags
specify amount of pages that sendfile may read ahead
when reading the file. A macro SF_FLAGS () is provided
to combine readahead amount and flags. An example showing specifying readahead
of 16 pages and SF_NOCACHE flag:
SF_FLAGS(16, SF_NOCACHE)
IMPLEMENTATION NOTESThe FreeBSD implementation ofsendfile () does not block on disk I/O when it sends a
file off the
ffs(7)
filesystem. The syscall returns success before the actual I/O completes, and
data is put into the socket later unattended. However, the order of data in
the socket is preserved, so it is safe to do further writes to the socket.
The FreeBSD implementation of
TUNINGphysical paging bufferssendfile () uses vnode pager to read file pages into
memory. The pager uses a pool of physical buffers to run its I/O operations.
When system runs out of pbufs, sendfile will block and report state
“zonelimit ”. Size of the pool can be
tuned with vm.vnode_pbufs
loader.conf(5)
tunable and can be checked with
sysctl(8)
OID of the same name at runtime.
sendfile(2) buffersOn some architectures, this system call internally uses a specialsendfile () buffer (struct
sf_buf) to handle sending file data to the client. If the sending socket
is blocking, and there are not enough sendfile ()
buffers available, sendfile () will block and report a
state of “sfbufa ”. If the sending socket
is non-blocking and there are not enough sendfile ()
buffers available, the call will block and wait for the necessary buffers to
become available before finishing the call.
The number of sf_buf's allocated should be
proportional to the number of nmbclusters used to send data to a client via
The number of If
sysctl(8)
OID kern.ipc.nsfbufs doesn't exist, your architecture
does not need to use RETURN VALUESThesendfile () function returns the value 0 if
successful; otherwise the value -1 is returned and the global variable
errno is set to indicate the error.
ERRORS
SEE ALSOnetstat(1), open(2), send(2), socket(2), writev(2), loader.conf(5), tuning(7), sysctl(8)K. Elmeleegy, A. Chanda, A. L. Cox, and W. Zwaenepoel, A Portable Kernel Abstraction for Low-Overhead Ephemeral Mapping Management, The Proceedings of the 2005 USENIX Annual Technical Conference, pp 223-236, 2005. HISTORYThesendfile () system call first appeared in
FreeBSD 3.0. This manual page first appeared in
FreeBSD 3.1. In FreeBSD 10
support for sending shared memory descriptors had been introduced. In
FreeBSD 11 a non-blocking implementation had been
introduced.
AUTHORSThe initial implementation ofsendfile () system call and
this manual page were written by David G. Lawrence
<dg@dglawrence.com>.
The FreeBSD 11 implementation was written by
Gleb Smirnoff <glebius@FreeBSD.org>. BUGSThesendfile () system call will not fail, i.e., return
-1 and set errno to
EFAULT , if provided an invalid address for
sbytes. The sendfile () system
call does not support SCTP sockets, it will return -1
and set errno to EINVAL .
Visit the GSP FreeBSD Man Page Interface. |