xref: /freebsd-14.2/lib/libc/sys/sendfile.2 (revision fffcbbcd)
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.Fd #include <sys/types.h>
38.Fd #include <sys/socket.h>
39.Fd #include <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
43.Fn Sendfile
44sends a regular file specified by descriptor
45.Fa fd
46out a stream socket specified by descriptor
47.Fa s .
48.Pp
49The
50.Fa offset
51argument specifies where to begin in the file.
52The
53.Fa nbytes
54argument specifies how many bytes of the file should be sent, with 0 having the special
55meaning of send until the end of file has been reached.
56.Pp
57An optional header and/or trailer can be sent before and after the file data by specifying
58a pointer to a struct sf_hdtr, which has the following structure:
59.Pp
60.Bd -literal -offset indent -compact
61struct sf_hdtr {
62	struct iovec *headers;	/* pointer to header iovecs */
63	int hdr_cnt;		/* number of header iovecs */
64	struct iovec *trailers;	/* pointer to trailer iovecs */
65	int trl_cnt;		/* number of trailer iovecs */
66};
67.Ed
68.Pp
69The
70.Fa headers
71and
72.Fa trailers
73pointers, if non-NULL, point to arrays of struct iovec structures.
74See the
75.Fn writev
76system call for information on the iovec structure.
77The number of iovecs in these
78arrays is specified by
79.Fa hdr_cnt
80and
81.Fa trl_cnt .
82.Pp
83If non-NULL, the system will write the total number of bytes sent on the socket to the
84variable pointed to by
85.Fa sbytes .
86.Pp
87The
88.Fa flags
89argument is currently undefined and should be specified as 0.
90.Pp
91When using a socket marked for non-blocking I/O,
92.Fn sendfile
93may send fewer bytes than requested.
94In this case, the number of bytes successfully
95written is returned in
96.Fa *sbytes
97(if specified),
98and the error
99.Er EAGAIN
100is returned.
101.Sh IMPLEMENTATION NOTES
102The
103.Fx
104implementation of
105.Fn sendfile
106is "zero-copy", meaning that it has been optimized so that copying of the file data is avoided.
107.Pp
108In the non-threaded library
109.Fn sendfile
110is implemented as the
111.Va sendfile
112syscall.
113.Pp
114In the threaded library, the
115.Va sendfile
116syscall is assembled to
117.Fn _thread_sys_sendfile
118and
119.Fn sendfile
120is implemented as a function which locks
121.Fa fd
122for reading and
123.Fa s
124for writing, then calls
125.Fn _thread_sys_sendfile .
126If the call to
127.Fn _thread_sys_sendfile
128would block, a context switch is performed.  Before returning,
129.Fn sendfile
130unlocks
131.Fa fd
132and
133.Fa s .
134.Sh RETURN VALUES
135Upon successful completion,
136.Fn sendfile
137returns 0. Otherwise a -1 is returned and the global variable
138.Va errno
139is set to indicate the error.
140.Sh ERRORS
141.Bl -tag -width Er
142.It Bq Er EBADF
143.Fa fd
144is not a valid file descriptor.
145.It Bq Er EBADF
146.Fa s
147is not a valid socket descriptor.
148.It Bq Er ENOTSOCK
149.Fa s
150is not a socket.
151.It Bq Er EINVAL
152.Fa fd
153is not a regular file.
154.It Bq Er EINVAL
155.Fa s
156is not a SOCK_STREAM type socket.
157.It Bq Er EINVAL
158.Fa offset
159is negative or out of range.
160.It Bq Er ENOTCONN
161.Fa s
162points to an unconnected socket.
163.It Bq Er EPIPE
164The socket peer has closed the connection.
165.It Bq Er EIO
166An error occurred while reading from
167.Fa fd .
168.It Bq Er EFAULT
169An invalid address was specified for a parameter.
170.It Bq Er EAGAIN
171The socket is marked for non-blocking I/O and not all data was sent due to the socket buffer being filled.
172If specified, the number of bytes successfully sent will be returned in
173.Fa *sbytes .
174.El
175.Sh SEE ALSO
176.Xr open 2 ,
177.Xr send 2 ,
178.Xr socket 2 ,
179.Xr writev 2
180.Sh HISTORY
181.Fn sendfile
182first appeared in
183.Fx 3.0 .
184This manual page first appeared in
185.Fx 3.1 .
186.Sh AUTHORS
187.Fn sendfile
188and this manual page were written by
189.An David Greenman Aq [email protected] .
190