xref: /freebsd-13.1/lib/libc/sys/sendfile.2 (revision 06a31d6a)
1.\" Copyright (c) 1998, David Greenman
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice unmodified, this list of conditions, and the following
9.\"    disclaimer.
10.\" 2. Redistributions in binary form must reproduce the above copyright
11.\"    notice, this list of conditions and the following disclaimer in the
12.\"    documentation and/or other materials provided with the distribution.
13.\"
14.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24.\" SUCH DAMAGE.
25.\"
26.\" $FreeBSD$
27.\"
28.Dd November 5, 1998
29.Dt SENDFILE 2
30.Os
31.Sh NAME
32.Nm sendfile
33.Nd send a file to a socket
34.Sh LIBRARY
35.Lb libc
36.Sh SYNOPSIS
37.In sys/types.h
38.In sys/socket.h
39.In sys/uio.h
40.Ft int
41.Fn sendfile "int fd" "int s" "off_t offset" "size_t nbytes" "struct sf_hdtr *hdtr" "off_t *sbytes" "int flags"
42.Sh DESCRIPTION
43The
44.Fn sendfile
45system call
46sends a regular file specified by descriptor
47.Fa fd
48out a stream socket specified by descriptor
49.Fa s .
50.Pp
51The
52.Fa offset
53argument specifies where to begin in the file.
54The
55.Fa nbytes
56argument specifies how many bytes of the file should be sent, with 0 having the special
57meaning of send until the end of file has been reached.
58.Pp
59An optional header and/or trailer can be sent before and after the file data by specifying
60a pointer to a struct sf_hdtr, which has the following structure:
61.Pp
62.Bd -literal -offset indent -compact
63struct sf_hdtr {
64	struct iovec *headers;	/* pointer to header iovecs */
65	int hdr_cnt;		/* number of header iovecs */
66	struct iovec *trailers;	/* pointer to trailer iovecs */
67	int trl_cnt;		/* number of trailer iovecs */
68};
69.Ed
70.Pp
71The
72.Fa headers
73and
74.Fa trailers
75pointers, if non-NULL, point to arrays of struct iovec structures.
76See the
77.Fn writev
78system call for information on the iovec structure.
79The number of iovecs in these
80arrays is specified by
81.Fa hdr_cnt
82and
83.Fa trl_cnt .
84.Pp
85If non-NULL, the system will write the total number of bytes sent on the socket to the
86variable pointed to by
87.Fa sbytes .
88.Pp
89The
90.Fa flags
91argument is currently undefined and should be specified as 0.
92.Pp
93When using a socket marked for non-blocking I/O,
94.Fn sendfile
95may send fewer bytes than requested.
96In this case, the number of bytes successfully
97written is returned in
98.Fa *sbytes
99(if specified),
100and the error
101.Er EAGAIN
102is returned.
103.Sh IMPLEMENTATION NOTES
104The
105.Fx
106implementation of
107.Fn sendfile
108is "zero-copy", meaning that it has been optimized so that copying of the file data is avoided.
109.Sh TUNING
110Internally, this system call uses a special
111.Xr sendfile 2
112buffer
113.Pq Vt "struct sf_buf"
114to handle sending file data to the client.
115If the sending socket is
116blocking, and there are not enough sendfile buffers available,
117.Xr sendfile 2
118will block and report a state of
119.Dq Li sfbufa .
120If the sending socket is non-blocking and there are not enough
121sendfile buffers available, the call will block and wait for the
122necessary buffers to become available before finishing the call.
123.Pp
124The number of
125.Vt sf_buf Ns 's
126allocated should be proportional to the number of nmbclusters used to
127send data to a client via
128.Xr sendfile 2 .
129Tune accordingly to avoid blocking!
130Busy installations that make extensive use of
131.Xr sendfile 2
132may want to increase these values to be inline with their
133.Va kern.ipc.nmbclusters
134(see
135.Xr tuning 7
136for details).
137.Pp
138The number of
139.Xr sendfile 2
140buffers in use is determined at boot time by either the
141.Va kern.ipc.nsfbufs
142.Xr loader.conf 5
143variable or the
144.Dv NSFBUFS
145kernel configuration tunable.
146The number of sendfile buffers scales with
147.Va kern.maxusers .
148.Sh RETURN VALUES
149.Rv -std sendfile
150.Sh ERRORS
151.Bl -tag -width Er
152.It Bq Er EBADF
153The
154.Fa fd
155argument
156is not a valid file descriptor.
157.It Bq Er EBADF
158The
159.Fa s
160argument
161is not a valid socket descriptor.
162.It Bq Er ENOTSOCK
163The
164.Fa s
165argument
166is not a socket.
167.It Bq Er EINVAL
168The
169.Fa fd
170argument
171is not a regular file.
172.It Bq Er EINVAL
173The
174.Fa s
175argument
176is not a SOCK_STREAM type socket.
177.It Bq Er EINVAL
178The
179.Fa offset
180argument
181is negative or out of range.
182.It Bq Er ENOTCONN
183The
184.Fa s
185argument
186points to an unconnected socket.
187.It Bq Er EPIPE
188The socket peer has closed the connection.
189.It Bq Er EIO
190An error occurred while reading from
191.Fa fd .
192.It Bq Er EFAULT
193An invalid address was specified for an argument.
194.It Bq Er EAGAIN
195The socket is marked for non-blocking I/O and not all data was sent due to the socket buffer being filled.
196If specified, the number of bytes successfully sent will be returned in
197.Fa *sbytes .
198.El
199.Sh SEE ALSO
200.Xr open 2 ,
201.Xr send 2 ,
202.Xr socket 2 ,
203.Xr writev 2 ,
204.Xr tuning 7
205.Sh HISTORY
206The
207.Fn sendfile
208system call
209first appeared in
210.Fx 3.0 .
211This manual page first appeared in
212.Fx 3.1 .
213.Sh AUTHORS
214The
215.Fn sendfile
216system call
217and this manual page were written by
218.An David Greenman Aq [email protected] .
219