SENDFILE(2) System Calls Manual SENDFILE(2)

sendfilesend a file to a socket

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>

int
sendfile(int fd, int s, off_t offset, off_t *len, struct sf_hdtr *hdtr, int flags);

The () system call sends a regular file 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 len argument is a value-result parameter, that specifies how many bytes of the file should be sent and/or how many bytes have been sent. Initially the value pointed to by the len argument specifies how many bytes should be sent with 0 having the special meaning to send until the end of file has been reached. On return the value pointed to by the len argument indicates how many bytes have been sent, except when a header or trailer is specified as shown below. The len pointer may not be NULL.

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-NULL, point to arrays of struct iovec structures. See the () system call for information on the iovec structure. The number of iovecs in these arrays is specified by hdr_cnt and trl_cnt.

When a header or trailer is specified, the value of len argument indicates the maximum number of bytes in the header and/or file to be sent. It does not control the trailer; if a trailer exists, all of it will be sent. If the value of len argument is 0, all of the header and/or file will be sent before the entire trailer is sent. On return, the len argument specifies the total number of bytes sent.

The flags parameter is reserved for future expansion and must be set to 0. Any other value will cause () to return EINVAL.

When using a socket marked for non-blocking I/O, () may send fewer bytes than requested. In this case, the number of bytes successfully sent is returned in the via the len parameters and the error EAGAIN is returned.

When a signal causes () to return the error EINTR, the len argument may return 0 without necessarily meaning the end of file has been reached as the signal may have been caught before any data was sent.

The Mac OS X implementation of sendfile() uses 64 bits types for size and offset parameters so there is no need for a 64 bits version sendfile64() as found on some other operating systems.

The sendfile() function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error.

The number of bytes sent is returned via the parameter len. A value of 0 means the end of the file specified by descriptor fd has been reached or that the value passed in offset falls beyond the end of file.

[]
The socket is marked for non-blocking I/O and not all data was sent due to the socket buffer being full. If specified, the number of bytes successfully sent will be returned in *len.
[]
The fd argument is not a valid file descriptor.
[]
The fd argument does not refer to a regular file.
[]
The s argument is not a valid socket descriptor.
[]
The s argument does not refer stream oriented socket.
[]
An invalid address was specified for an argument.
[]
A signal interrupted sendfile() before it could be completed. If specified, the number of bytes successfully sent will be returned in *len.
[]
The offset argument is negative.
[]
The len argument is NULL.
[]
The flags argument is not set to 0.
[]
An error occurred while reading from fd.
[]
The s argument points to an unconnected socket.
[]
The s argument is not a socket.
[]
The file system for descriptor fd does not support sendfile().
[]
The socket peer has closed the connection.

open(2), send(2), socket(2), writev(2)

The sendfile() system call first appeared in Darwin 9.0 (Mac OS X version 10.5) .

This manual page is based on the FreeBSD version written by David G. Lawrence ⟨dg@dglawrence.com⟩

March 31, 2006 Mac OS X