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 "May 31, 2006"
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);
21 The \fBsendfile()\fR function copies data from \fIin_fd\fR to \fIout_fd\fR
22 starting at offset \fIoff\fR and of length \fIlen\fR bytes. The \fIin_fd\fR
23 argument should be a file descriptor to a regular file opened for reading. See
24 \fBopen\fR(2). The \fIout_fd\fR argument should be a file descriptor to a
25 regular file opened for writing or to a connected \fBAF_INET\fR or
26 \fBAF_INET6\fR socket of \fBSOCK_STREAM\fR type. See \fBsocket\fR(3SOCKET). The
27 \fIoff\fR argument is a pointer to a variable holding the input file pointer
28 position from which the data will be read. After \fBsendfile()\fR has
29 completed, the variable will be set to the offset of the byte following the
30 last byte that was read. The \fBsendfile()\fR function does not modify the
31 current file pointer of \fIin_fd\fR, but does modify the file pointer for
32 \fIout_fd\fR if it is a regular file.
35 The \fBsendfile()\fR function can also be used to send buffers by pointing
36 \fIin_fd\fR to \fBSFV_FD_SELF\fR.
40 Upon successful completion, \fBsendfile()\fR returns the total number of bytes
41 written to \fIout_fd\fR and also updates the offset to point to the byte that
42 follows the last byte read. Otherwise, it returns \fB-1\fR, and \fBerrno\fR is
43 set to indicate the error.
47 The \fBsendfile()\fR function will fail if:
51 \fB\fBEAFNOSUPPORT\fR\fR
54 The implementation does not support the specified address family for socket.
63 Mandatory file or record locking is set on either the file descriptor or output
64 file descriptor if it points at regular files. \fBO_NDELAY\fR or
65 \fBO_NONBLOCK\fR is set, and there is a blocking record lock. An attempt has
66 been made to write to a stream that cannot accept data with the \fBO_NDELAY\fR
67 or the \fBO_NONBLOCK\fR flag set.
76 The \fIout_fd\fR or \fIin_fd\fR argument is either not a valid file descriptor,
77 \fIout_fd\fR is not opened for writing. or \fIin_fd\fR is not opened for
87 The offset cannot be represented by the \fBoff_t\fR structure, or the length is
88 negative when cast to \fBssize_t\fR.
97 An I/O error occurred while accessing the file system.
106 The socket is not connected.
112 \fB\fBEOPNOTSUPP\fR\fR
115 The socket type is not supported.
124 The \fIout_fd\fR argument is no longer connected to the peer endpoint.
133 A signal was caught during the write operation and no data was transferred.
139 The \fBsendfile()\fR function has a transitional interface for 64-bit file
140 offsets. See \fBlf64\fR(5).
143 \fBExample 1 \fRSending a Buffer Over a Socket
146 The following example demonstrates how to send the buffer \fIbuf\fR over a
147 socket. At the end, it prints the number of bytes transferred over the socket
148 from the buffer. It assumes that \fIaddr\fR will be filled up appropriately,
149 depending upon where to send the buffer.
156 struct sockaddr_in sin;
161 tfd = socket(AF_INET, SOCK_STREAM, 0);
167 sin.sin_family = AF_INET;
168 sin.sin_addr.s_addr = addr; /* Fill in the appropriate address. */
169 sin.sin_port = htons(2345);
170 if (connect(tfd, (struct sockaddr *)&sin, sizeof(sin))<0) {
179 res = sendfile(tfd, SFV_FD_SELF, &baddr, len);
181 if (errno != EINTR) {
191 \fBExample 2 \fRTransferring Files to Sockets
194 The following program demonstrates a transfer of files to sockets:
201 struct sockaddr_in sin;
204 struct stat stat_buf;
207 ffd = open("file", O_RDONLY);
213 tfd = socket(AF_INET, SOCK_STREAM, 0);
219 sin.sin_family = AF_INET;
220 sin.sin_addr = addr; /* Fill in the appropriate address. */
221 sin.sin_port = htons(2345);
222 if (connect(tfd, (struct sockaddr *) &sin, sizeof(sin)) <0) {
227 if (fstat(ffd, &stat_buf) == -1) {
232 len = stat_buf.st_size;
235 res = sendfile(tfd, ffd, &off, len);
237 if (errno != EINTR) {
249 See \fBattributes\fR(5) for descriptions of the following attributes:
257 ATTRIBUTE TYPE ATTRIBUTE VALUE
259 Interface Stability Evolving
267 \fBopen\fR(2), \fBlibsendfile\fR(3LIB), \fBsendfilev\fR(3EXT),
268 \fBsocket\fR(3SOCKET), \fBattributes\fR(5), \fBlf64\fR(5)