SENDFILE(2)

NAME
sendfile - send a file to a socket

LIBRARY
libc

SYNOPSIS
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
int
sendfile(int fd, int s, off_t offset, size_t nbytes, struct sf_hdtr *hdtr, off_t *sbytes, int flags);

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

If non-NULL, the system will write the total number of bytes sent on the socket to the variable pointed to by sbytes.

The flags argument is currently undefined and should be specified as 0.

When using a socket marked for non-blocking I/O, sendfile() may send fewer bytes than requested. In this case, the number of bytes successfully written is returned in *sbytes (if specified), and the error EAGAIN is returned.

IMPLEMENTATION NOTES
The DragonFly implementation of sendfile() is "zero-copy", meaning that it has been optimized so that copying of the file data is avoided.

RETURN VALUES

ERRORS

[EBADF]
    fd is not a valid file descriptor.

[EBADF]
    s is not a valid socket descriptor.

[ENOTSOCK]
    s is not a socket.

[EINVAL]
    fd is not a regular file.

[EINVAL]
    s is not a SOCK_STREAM type socket.

[EINVAL]
    offset is negative or out of range.

[ENOTCONN]
    s points to an unconnected socket.

[EPIPE]
    The socket peer has closed the connection.

[EIO]
    An error occurred while reading from fd.

[EFAULT]
    An invalid address was specified for a parameter.

[EAGAIN]
    The socket is marked for non-blocking I/O and not all data was sent due to the socket buffer being filled. If specified, the number of bytes successfully sent will be returned in *sbytes.

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

HISTORY
sendfile() first appeared in FreeBSD 3.0. This manual page first appeared in FreeBSD 3.1.

AUTHORS
sendfile() and this manual page were written by David Greenman <dg@root.com>.