freebsd-skq/lib/libc/sys/sendfile.2

240 lines
6.3 KiB
Groff
Raw Normal View History

.\" Copyright (c) 2003, David G. Lawrence
1998-11-05 14:43:29 +00:00
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice unmodified, this list of conditions, and the following
.\" disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
1999-08-28 00:22:10 +00:00
.\" $FreeBSD$
1998-11-05 14:43:29 +00:00
.\"
.Dd November 5, 1998
.Dt SENDFILE 2
.Os
.Sh NAME
.Nm sendfile
.Nd send a file to a socket
.Sh LIBRARY
.Lb libc
1998-11-05 14:43:29 +00:00
.Sh SYNOPSIS
.In sys/types.h
.In sys/socket.h
.In sys/uio.h
1998-11-05 14:43:29 +00:00
.Ft int
.Fn sendfile "int fd" "int s" "off_t offset" "size_t nbytes" "struct sf_hdtr *hdtr" "off_t *sbytes" "int flags"
.Sh DESCRIPTION
The
.Fn sendfile
system call
1998-11-05 14:43:29 +00:00
sends a regular file specified by descriptor
.Fa fd
out a stream socket specified by descriptor
.Fa s .
.Pp
The
.Fa offset
argument specifies where to begin in the file.
The
1998-11-05 14:43:29 +00:00
.Fa 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.
.Pp
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:
.Pp
.Bd -literal -offset indent -compact
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 */
};
.Ed
.Pp
The
.Fa headers
and
2001-02-04 02:05:16 +00:00
.Fa trailers
pointers, if non-NULL, point to arrays of struct iovec structures.
See the
1998-11-05 14:43:29 +00:00
.Fn writev
system call for information on the iovec structure.
The number of iovecs in these
1998-11-05 14:43:29 +00:00
arrays is specified by
.Fa hdr_cnt
and
.Fa trl_cnt .
.Pp
If non-NULL, the system will write the total number of bytes sent on the socket to the
variable pointed to by
.Fa sbytes .
.Pp
The
.Fa flags
argument has one possible value:
.Fa SF_NODISKIO .
This flag causes any sendfile call which would block on disk I/O to instead
return EBUSY. Busy servers may benefit by transferring requests that would
block to a seperate I/O worker thread.
1998-11-06 19:35:58 +00:00
.Pp
When using a socket marked for non-blocking I/O,
.Fn sendfile
may send fewer bytes than requested.
In this case, the number of bytes successfully
1998-11-06 19:35:58 +00:00
written is returned in
.Fa *sbytes
(if specified),
and the error
.Er EAGAIN
is returned.
1998-11-05 14:43:29 +00:00
.Sh IMPLEMENTATION NOTES
2000-11-14 11:20:58 +00:00
The
.Fx
implementation of
1998-11-05 14:43:29 +00:00
.Fn sendfile
is "zero-copy", meaning that it has been optimized so that copying of the file data is avoided.
.Sh TUNING
Internally, this system call uses a special
.Xr sendfile 2
buffer
.Pq Vt "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,
.Xr sendfile 2
will block and report a state of
.Dq Li 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.
.Pp
The number of
.Vt sf_buf Ns 's
allocated should be proportional to the number of nmbclusters used to
send data to a client via
.Xr sendfile 2 .
Tune accordingly to avoid blocking!
Busy installations that make extensive use of
.Xr sendfile 2
may want to increase these values to be inline with their
.Va kern.ipc.nmbclusters
(see
.Xr tuning 7
for details).
.Pp
The number of
.Xr sendfile 2
buffers available is determined at boot time by either the
.Va kern.ipc.nsfbufs
.Xr loader.conf 5
variable or the
.Dv NSFBUFS
kernel configuration tunable.
The number of sendfile buffers scales with
.Va kern.maxusers .
.Va kern.ipc.nsfbufsused
and
.Va kern.ipc.nsfbufspeak
read-only sysctl variables show current and peak
.Xr sendfile 2
buffers usage respectively.
These values may also be viewed through
.Nm netstat Fl m .
1998-11-05 14:43:29 +00:00
.Sh RETURN VALUES
.Rv -std sendfile
1998-11-05 14:43:29 +00:00
.Sh ERRORS
.Bl -tag -width Er
.It Bq Er EBADF
2002-12-19 09:40:28 +00:00
The
1998-11-05 14:43:29 +00:00
.Fa fd
2002-12-19 09:40:28 +00:00
argument
1998-11-05 14:43:29 +00:00
is not a valid file descriptor.
.It Bq Er EBADF
2002-12-19 09:40:28 +00:00
The
1998-11-05 14:43:29 +00:00
.Fa s
2002-12-19 09:40:28 +00:00
argument
1998-11-05 14:43:29 +00:00
is not a valid socket descriptor.
.It Bq Er EBUSY
Completing the entire transfer would have required disk I/O, so
it was aborted. Partial data may have been sent.
(This error can only occur when SF_NODISKIO is specified.)
1998-11-05 14:43:29 +00:00
.It Bq Er ENOTSOCK
2002-12-19 09:40:28 +00:00
The
1998-11-05 14:43:29 +00:00
.Fa s
2002-12-19 09:40:28 +00:00
argument
1998-11-05 14:43:29 +00:00
is not a socket.
.It Bq Er EINVAL
2002-12-19 09:40:28 +00:00
The
1998-11-05 14:43:29 +00:00
.Fa fd
2002-12-19 09:40:28 +00:00
argument
1998-11-05 14:43:29 +00:00
is not a regular file.
.It Bq Er EINVAL
2002-12-19 09:40:28 +00:00
The
1998-11-05 14:43:29 +00:00
.Fa s
2002-12-19 09:40:28 +00:00
argument
1998-11-05 14:43:29 +00:00
is not a SOCK_STREAM type socket.
.It Bq Er EINVAL
2002-12-19 09:40:28 +00:00
The
1998-11-05 14:43:29 +00:00
.Fa offset
2002-12-19 09:40:28 +00:00
argument
1998-11-05 14:43:29 +00:00
is negative or out of range.
.It Bq Er ENOTCONN
2002-12-19 09:40:28 +00:00
The
1998-11-05 14:43:29 +00:00
.Fa s
2002-12-19 09:40:28 +00:00
argument
1998-11-05 14:43:29 +00:00
points to an unconnected socket.
.It Bq Er EPIPE
The socket peer has closed the connection.
.It Bq Er EIO
An error occurred while reading from
.Fa fd .
.It Bq Er EFAULT
2002-12-19 09:40:28 +00:00
An invalid address was specified for an argument.
.It Bq Er EINTR
A signal interrupted sendfile before it could be completed. If specified, the number
of bytes successfully sent will be returned in
.Fa *sbytes .
1998-11-06 19:35:58 +00:00
.It Bq Er 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
.Fa *sbytes .
1998-11-05 14:43:29 +00:00
.El
.Sh SEE ALSO
.Xr netstat 1 ,
1998-11-05 14:43:29 +00:00
.Xr open 2 ,
.Xr send 2 ,
.Xr socket 2 ,
.Xr writev 2 ,
.Xr tuning 7
1998-11-05 14:43:29 +00:00
.Sh HISTORY
The
1998-11-05 14:43:29 +00:00
.Fn sendfile
system call
first appeared in
.Fx 3.0 .
This manual page first appeared in
.Fx 3.1 .
1998-11-05 14:43:29 +00:00
.Sh AUTHORS
The
1998-11-05 14:43:29 +00:00
.Fn sendfile
system call
and this manual page were written by
.An David G. Lawrence Aq dg@dglawrence.com .