2 .\" Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
3 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
4 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
5 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
6 .TH SENDFILE 3EXT "Nov 3, 2017"
8 sendfile \- send files over sockets or copy files to files
12 \fBcc\fR [ \fIflag\fR\&.\|.\|. ] \fIfile\fR\&.\|.\|. \fB-lsendfile\fR [ \fIlibrary\fR\&.\|.\|. ]
13 #include <sys/sendfile.h>
15 \fBssize_t\fR \fBsendfile\fR(\fBint\fR \fIout_fd\fR, \fBint\fR \fIin_fd\fR, \fBoff_t *\fR\fIoff\fR, \fBsize_t\fR \fIlen\fR);
20 The \fBsendfile()\fR function copies data from \fIin_fd\fR to \fIout_fd\fR
21 starting at offset \fIoff\fR and of length \fIlen\fR bytes. The \fIin_fd\fR
22 argument should be a file descriptor to a regular file opened for reading. See
23 \fBopen\fR(2). The \fIout_fd\fR argument should be a file descriptor to a
24 regular file opened for writing or to a connected \fBAF_INET\fR or
25 \fBAF_INET6\fR socket of \fBSOCK_STREAM\fR type. See \fBsocket\fR(3SOCKET). The
26 \fIoff\fR argument is a pointer to a variable holding the input file pointer
27 position from which the data will be read. After \fBsendfile()\fR has
28 completed, the variable will be set to the offset of the byte following the
29 last byte that was read. The \fBsendfile()\fR function does not modify the
30 current file pointer of \fIin_fd\fR, but does modify the file pointer for
31 \fIout_fd\fR if it is a regular file.
34 The \fBsendfile()\fR function can also be used to send buffers by pointing
35 \fIin_fd\fR to \fBSFV_FD_SELF\fR.
38 Upon successful completion, \fBsendfile()\fR returns the total number of bytes
39 written to \fIout_fd\fR and also updates the offset to point to the byte that
40 follows the last byte read. Otherwise, it returns \fB-1\fR, and \fBerrno\fR is
41 set to indicate the error.
44 The \fBsendfile()\fR function will fail if:
48 \fB\fBEAFNOSUPPORT\fR\fR
51 The implementation does not support the specified address family for socket.
60 Mandatory file or record locking is set on either the file descriptor or output
61 file descriptor if it points at regular files. \fBO_NDELAY\fR or
62 \fBO_NONBLOCK\fR is set, and there is a blocking record lock. An attempt has
63 been made to write to a stream that cannot accept data with the \fBO_NDELAY\fR
64 or the \fBO_NONBLOCK\fR flag set.
73 The \fIout_fd\fR or \fIin_fd\fR argument is either not a valid file descriptor,
74 \fIout_fd\fR is not opened for writing. or \fIin_fd\fR is not opened for
84 The offset cannot be represented by the \fBoff_t\fR structure, or the length is
85 negative when cast to \fBssize_t\fR.
94 An I/O error occurred while accessing the file system.
103 The socket is not connected.
109 \fB\fBEOPNOTSUPP\fR\fR
112 The socket type is not supported.
121 The \fIout_fd\fR argument is no longer connected to the peer endpoint.
122 The \fBSIGPIPE\fR signal is generated to the calling thread.
123 The process dies unless special provisions were taken to catch or ignore
133 A signal was caught during the write operation and no data was transferred.
138 The \fBsendfile()\fR function has a transitional interface for 64-bit file
139 offsets. See \fBlf64\fR(5).
142 \fBExample 1 \fRSending a Buffer Over a Socket
145 The following example demonstrates how to send the buffer \fIbuf\fR over a
146 socket. At the end, it prints the number of bytes transferred over the socket
147 from the buffer. It assumes that \fIaddr\fR will be filled up appropriately,
148 depending upon where to send the buffer.
155 struct sockaddr_in sin;
160 tfd = socket(AF_INET, SOCK_STREAM, 0);
166 sin.sin_family = AF_INET;
167 sin.sin_addr.s_addr = addr; /* Fill in the appropriate address. */
168 sin.sin_port = htons(2345);
169 if (connect(tfd, (struct sockaddr *)&sin, sizeof(sin))<0) {
178 res = sendfile(tfd, SFV_FD_SELF, &baddr, len);
180 if (errno != EINTR) {
190 \fBExample 2 \fRTransferring Files to Sockets
193 The following program demonstrates a transfer of files to sockets:
200 struct sockaddr_in sin;
203 struct stat stat_buf;
206 ffd = open("file", O_RDONLY);
212 tfd = socket(AF_INET, SOCK_STREAM, 0);
218 sin.sin_family = AF_INET;
219 sin.sin_addr = addr; /* Fill in the appropriate address. */
220 sin.sin_port = htons(2345);
221 if (connect(tfd, (struct sockaddr *) &sin, sizeof(sin)) <0) {
226 if (fstat(ffd, &stat_buf) == -1) {
231 len = stat_buf.st_size;
234 res = sendfile(tfd, ffd, &off, len);
236 if (errno != EINTR) {
247 See \fBattributes\fR(5) for descriptions of the following attributes:
255 ATTRIBUTE TYPE ATTRIBUTE VALUE
257 Interface Stability Evolving
264 \fBopen\fR(2), \fBlibsendfile\fR(3LIB), \fBsendfilev\fR(3EXT),
265 \fBsocket\fR(3SOCKET), \fBattributes\fR(5), \fBlf64\fR(5)