1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
'\" te
.\" Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
.\" 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.
.\" 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.
.\" 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]
.TH SENDFILE 3EXT "Nov 3, 2017"
.SH NAME
sendfile \- send files over sockets or copy files to files
.SH SYNOPSIS
.LP
.nf
\fBcc\fR [ \fIflag\fR\&.\|.\|. ] \fIfile\fR\&.\|.\|. \fB-lsendfile\fR [ \fIlibrary\fR\&.\|.\|. ]
#include <sys/sendfile.h>
\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);
.fi
.SH DESCRIPTION
.LP
The \fBsendfile()\fR function copies data from \fIin_fd\fR to \fIout_fd\fR
starting at offset \fIoff\fR and of length \fIlen\fR bytes. The \fIin_fd\fR
argument should be a file descriptor to a regular file opened for reading. See
\fBopen\fR(2). The \fIout_fd\fR argument should be a file descriptor to a
regular file opened for writing or to a connected \fBAF_INET\fR or
\fBAF_INET6\fR socket of \fBSOCK_STREAM\fR type. See \fBsocket\fR(3SOCKET). The
\fIoff\fR argument is a pointer to a variable holding the input file pointer
position from which the data will be read. After \fBsendfile()\fR has
completed, the variable will be set to the offset of the byte following the
last byte that was read. The \fBsendfile()\fR function does not modify the
current file pointer of \fIin_fd\fR, but does modify the file pointer for
\fIout_fd\fR if it is a regular file.
.sp
.LP
The \fBsendfile()\fR function can also be used to send buffers by pointing
\fIin_fd\fR to \fBSFV_FD_SELF\fR.
.SH RETURN VALUES
.LP
Upon successful completion, \fBsendfile()\fR returns the total number of bytes
written to \fIout_fd\fR and also updates the offset to point to the byte that
follows the last byte read. Otherwise, it returns \fB-1\fR, and \fBerrno\fR is
set to indicate the error.
.SH ERRORS
.LP
The \fBsendfile()\fR function will fail if:
.sp
.ne 2
.na
\fB\fBEAFNOSUPPORT\fR\fR
.ad
.RS 16n
The implementation does not support the specified address family for socket.
.RE
.sp
.ne 2
.na
\fB\fBEAGAIN\fR\fR
.ad
.RS 16n
Mandatory file or record locking is set on either the file descriptor or output
file descriptor if it points at regular files. \fBO_NDELAY\fR or
\fBO_NONBLOCK\fR is set, and there is a blocking record lock. An attempt has
been made to write to a stream that cannot accept data with the \fBO_NDELAY\fR
or the \fBO_NONBLOCK\fR flag set.
.RE
.sp
.ne 2
.na
\fB\fBEBADF\fR\fR
.ad
.RS 16n
The \fIout_fd\fR or \fIin_fd\fR argument is either not a valid file descriptor,
\fIout_fd\fR is not opened for writing. or \fIin_fd\fR is not opened for
reading.
.RE
.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 16n
The offset cannot be represented by the \fBoff_t\fR structure, or the length is
negative when cast to \fBssize_t\fR.
.RE
.sp
.ne 2
.na
\fB\fBEIO\fR\fR
.ad
.RS 16n
An I/O error occurred while accessing the file system.
.RE
.sp
.ne 2
.na
\fB\fBENOTCONN\fR\fR
.ad
.RS 16n
The socket is not connected.
.RE
.sp
.ne 2
.na
\fB\fBEOPNOTSUPP\fR\fR
.ad
.RS 16n
The socket type is not supported.
.RE
.sp
.ne 2
.na
\fB\fBEPIPE\fR\fR
.ad
.RS 16n
The \fIout_fd\fR argument is no longer connected to the peer endpoint.
The \fBSIGPIPE\fR signal is generated to the calling thread.
The process dies unless special provisions were taken to catch or ignore
the signal.
.RE
.sp
.ne 2
.na
\fB\fBEINTR\fR\fR
.ad
.RS 16n
A signal was caught during the write operation and no data was transferred.
.RE
.SH USAGE
.LP
The \fBsendfile()\fR function has a transitional interface for 64-bit file
offsets. See \fBlf64\fR(5).
.SH EXAMPLES
.LP
\fBExample 1 \fRSending a Buffer Over a Socket
.sp
.LP
The following example demonstrates how to send the buffer \fIbuf\fR over a
socket. At the end, it prints the number of bytes transferred over the socket
from the buffer. It assumes that \fIaddr\fR will be filled up appropriately,
depending upon where to send the buffer.
.sp
.in +2
.nf
int tfd;
off_t baddr;
struct sockaddr_in sin;
char buf[64 * 1024];
in_addr_t addr;
size_t len;
tfd = socket(AF_INET, SOCK_STREAM, 0);
if (tfd == -1) {
perror("socket");
exit(1);
}
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = addr; /* Fill in the appropriate address. */
sin.sin_port = htons(2345);
if (connect(tfd, (struct sockaddr *)&sin, sizeof(sin))<0) {
perror("connect");
exit(1);
}
baddr = (off_t)buf;
len = sizeof(buf);
while (len > 0) {
ssize_t res;
res = sendfile(tfd, SFV_FD_SELF, &baddr, len);
if (res == -1)
if (errno != EINTR) {
perror("sendfile");
exit(1);
} else continue;
len -= res;
}
.fi
.in -2
.LP
\fBExample 2 \fRTransferring Files to Sockets
.sp
.LP
The following program demonstrates a transfer of files to sockets:
.sp
.in +2
.nf
int ffd, tfd;
off_t off;
struct sockaddr_in sin;
in_addr_t addr;
int len;
struct stat stat_buf;
ssize_t len;
ffd = open("file", O_RDONLY);
if (ffd == -1) {
perror("open");
exit(1);
}
tfd = socket(AF_INET, SOCK_STREAM, 0);
if (tfd == -1) {
perror("socket");
exit(1);
}
sin.sin_family = AF_INET;
sin.sin_addr = addr; /* Fill in the appropriate address. */
sin.sin_port = htons(2345);
if (connect(tfd, (struct sockaddr *) &sin, sizeof(sin)) <0) {
perror("connect");
exit(1);
}
if (fstat(ffd, &stat_buf) == -1) {
perror("fstat");
exit(1);
}
len = stat_buf.st_size;
while (len > 0) {
ssize_t res;
res = sendfile(tfd, ffd, &off, len);
if (res == -1)
if (errno != EINTR) {
perror("sendfile");
exit(1);
} else continue;
len -= res;
}
.fi
.in -2
.SH ATTRIBUTES
.LP
See \fBattributes\fR(5) for descriptions of the following attributes:
.sp
.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE ATTRIBUTE VALUE
_
Interface Stability Evolving
_
MT-Level MT-Safe
.TE
.SH SEE ALSO
.LP
\fBopen\fR(2), \fBlibsendfile\fR(3LIB), \fBsendfilev\fR(3EXT),
\fBsocket\fR(3SOCKET), \fBattributes\fR(5), \fBlf64\fR(5)
|