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 "Jul 19, 2018"
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. In some error cases \fBsendfile()\fR may still
42 write some data before encountering an error and returning \fB-1\fR. When that
43 occurs, \fIoff\fR is updated to point to the byte that follows the last byte
44 copied and should be compared with its value before calling \fBsendfile()\fR to
45 determine how much data was sent.
48 The \fBsendfile()\fR function will fail if:
52 \fB\fBEAFNOSUPPORT\fR\fR
55 The implementation does not support the specified address family for socket.
64 Mandatory file or record locking is set on either the file descriptor or output
65 file descriptor if it points at regular files. \fBO_NDELAY\fR or
66 \fBO_NONBLOCK\fR is set, and there is a blocking record lock. An attempt has
67 been made to write to a stream that cannot accept data with the \fBO_NDELAY\fR
68 or the \fBO_NONBLOCK\fR flag set.
77 The \fIout_fd\fR or \fIin_fd\fR argument is either not a valid file descriptor,
78 \fIout_fd\fR is not opened for writing. or \fIin_fd\fR is not opened for
88 The offset cannot be represented by the \fBoff_t\fR structure, or the length is
89 negative when cast to \fBssize_t\fR.
91 Fewer bytes were transferred than were requested.
100 An I/O error occurred while accessing the file system.
109 The socket is not connected.
115 \fB\fBEOPNOTSUPP\fR\fR
118 The socket type is not supported.
127 The \fIout_fd\fR argument is no longer connected to the peer endpoint.
128 The \fBSIGPIPE\fR signal is generated to the calling thread.
129 The process dies unless special provisions were taken to catch or ignore
139 A signal was caught during the write operation and no data was transferred.
144 The \fBsendfile()\fR function has a transitional interface for 64-bit file
145 offsets. See \fBlf64\fR(5).
148 \fBExample 1 \fRSending a Buffer Over a Socket
151 The following example demonstrates how to send the buffer \fIbuf\fR over a
152 socket. At the end, it prints the number of bytes transferred over the socket
153 from the buffer. It assumes that \fIaddr\fR will be filled up appropriately,
154 depending upon where to send the buffer.
161 struct sockaddr_in sin;
166 tfd = socket(AF_INET, SOCK_STREAM, 0);
172 sin.sin_family = AF_INET;
173 sin.sin_addr.s_addr = addr; /* Fill in the appropriate address. */
174 sin.sin_port = htons(2345);
175 if (connect(tfd, (struct sockaddr *)&sin, sizeof(sin))<0) {
184 res = sendfile(tfd, SFV_FD_SELF, &baddr, len);
186 if (errno != EINTR) {
196 \fBExample 2 \fRTransferring Files to Sockets
199 The following program demonstrates a transfer of files to sockets:
206 struct sockaddr_in sin;
209 struct stat stat_buf;
212 ffd = open("file", O_RDONLY);
218 tfd = socket(AF_INET, SOCK_STREAM, 0);
224 sin.sin_family = AF_INET;
225 sin.sin_addr = addr; /* Fill in the appropriate address. */
226 sin.sin_port = htons(2345);
227 if (connect(tfd, (struct sockaddr *) &sin, sizeof(sin)) <0) {
232 if (fstat(ffd, &stat_buf) == -1) {
237 len = stat_buf.st_size;
240 res = sendfile(tfd, ffd, &off, len);
242 if (errno != EINTR) {
253 See \fBattributes\fR(5) for descriptions of the following attributes:
261 ATTRIBUTE TYPE ATTRIBUTE VALUE
263 Interface Stability Evolving
270 \fBopen\fR(2), \fBlibsendfile\fR(3LIB), \fBsendfilev\fR(3EXT),
271 \fBsocket\fR(3SOCKET), \fBattributes\fR(5), \fBlf64\fR(5)