CUPS merge from SAMBA_2_2
[Samba/ekacnet.git] / source / lib / sendfile.c
blobdf6c1980d061219d1a109820371bea8a4d2e0c07
1 /*
2 Unix SMB/Netbios implementation.
3 Version 2.2.x / 3.0.x
4 sendfile implementations.
5 Copyright (C) Jeremy Allison 2002.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * This file handles the OS dependent sendfile implementations.
23 * The API is such that it returns -1 on error, else returns the
24 * number of bytes written.
27 #include "includes.h"
29 #if defined(LINUX_SENDFILE_API)
31 #include <sys/sendfile.h>
33 #ifndef MSG_MORE
34 #define MSG_MORE 0x8000
35 #endif
37 ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
39 size_t total=0;
40 ssize_t ret;
41 ssize_t hdr_len = 0;
44 * Send the header first.
45 * Use MSG_MORE to cork the TCP output until sendfile is called.
48 if (header) {
49 hdr_len = header->length;
50 while (total < hdr_len) {
51 ret = sys_send(tofd, header->data + total,hdr_len - total, MSG_MORE);
52 if (ret == -1)
53 return -1;
54 total += ret;
58 total = count;
59 while (total) {
60 ssize_t nwritten;
61 do {
62 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_SENDFILE64)
63 nwritten = sendfile64(tofd, fromfd, &offset, total);
64 #else
65 nwritten = sendfile(tofd, fromfd, &offset, total);
66 #endif
67 } while (nwritten == -1 && errno == EINTR);
68 if (nwritten == -1)
69 return -1;
70 if (nwritten == 0)
71 return -1; /* I think we're at EOF here... */
72 total -= nwritten;
74 return count + hdr_len;
77 #elif defined(LINUX_BROKEN_SENDFILE_API)
80 * We must use explicit 32 bit types here. This code path means Linux
81 * won't do proper 64-bit sendfile. JRA.
84 extern int32 sendfile (int out_fd, int in_fd, int32 *offset, uint32 count);
87 #ifndef MSG_MORE
88 #define MSG_MORE 0x8000
89 #endif
91 ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
93 size_t total=0;
94 ssize_t ret;
95 ssize_t hdr_len = 0;
96 uint32 small_total = 0;
97 int32 small_offset;
99 /*
100 * Fix for broken Linux 2.4 systems with no working sendfile64().
101 * If the offset+count > 2 GB then pretend we don't have the
102 * system call sendfile at all. The upper layer catches this
103 * and uses a normal read. JRA.
106 if ((sizeof(SMB_OFF_T) >= 8) && (offset + count > (SMB_OFF_T)0x7FFFFFFF)) {
107 errno = ENOSYS;
108 return -1;
112 * Send the header first.
113 * Use MSG_MORE to cork the TCP output until sendfile is called.
116 if (header) {
117 hdr_len = header->length;
118 while (total < hdr_len) {
119 ret = sys_send(tofd, header->data + total,hdr_len - total, MSG_MORE);
120 if (ret == -1)
121 return -1;
122 total += ret;
126 small_total = (uint32)count;
127 small_offset = (int32)offset;
129 while (small_total) {
130 int32 nwritten;
131 do {
132 nwritten = sendfile(tofd, fromfd, &small_offset, small_total);
133 } while (nwritten == -1 && errno == EINTR);
134 if (nwritten == -1)
135 return -1;
136 if (nwritten == 0)
137 return -1; /* I think we're at EOF here... */
138 small_total -= nwritten;
140 return count + hdr_len;
144 #elif defined(SOLARIS_SENDFILE_API)
146 /* Hmmm. Can't find Solaris sendfile API docs.... Where is it ? */
147 ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
149 errno = ENOSYS;
150 return -1;
153 #elif defined(HPUX_SENDFILE_API)
155 #include <sys/socket.h>
156 #include <sys/uio.h>
158 ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
160 size_t total=0;
161 struct iovec hdtrl[2];
162 size_t hdr_len = 0;
164 if (header) {
165 /* Set up the header/trailer iovec. */
166 hdtrl[0].iov_base = header->data;
167 hdtrl[0].iov_len = hdr_len = header->length;
168 } else {
169 hdtrl[0].iov_base = NULL;
170 hdtrl[0].iov_len = hdr_len = 0;
172 hdtrl[1].iov_base = NULL;
173 hdtrl[1].iov_base = 0;
175 total = count;
176 while (total + hdtrl[0].iov_len) {
177 ssize_t nwritten;
180 * HPUX guarantees that if any data was written before
181 * a signal interrupt then sendfile returns the number of
182 * bytes written (which may be less than requested) not -1.
183 * nwritten includes the header data sent.
186 do {
187 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_SENDFILE64)
188 nwritten = sendfile64(tofd, fromfd, offset, total, &hdtrl[0], 0);
189 #else
190 nwritten = sendfile(tofd, fromfd, offset, total, &hdtrl[0], 0);
191 #endif
192 } while (nwritten == -1 && errno == EINTR);
193 if (nwritten == -1)
194 return -1;
195 if (nwritten == 0)
196 return -1; /* I think we're at EOF here... */
199 * If this was a short (signal interrupted) write we may need
200 * to subtract it from the header data, or null out the header
201 * data altogether if we wrote more than hdtrl[0].iov_len bytes.
202 * We change nwritten to be the number of file bytes written.
205 if (hdtrl[0].iov_base && hdtrl[0].iov_len) {
206 if (nwritten >= hdtrl[0].iov_len) {
207 nwritten -= hdtrl[0].iov_len;
208 hdtrl[0].iov_base = NULL;
209 hdtrl[0].iov_len = 0;
210 } else {
211 nwritten = 0;
212 /* iov_base is defined as a void *... */
213 hdtrl[0].iov_base = ((char *)hdtrl[0].iov_base) + nwritten;
214 hdtrl[0].iov_len -= nwritten;
217 total -= nwritten;
218 offset += nwritten;
220 return count + hdr_len;
223 #elif defined(FREEBSD_SENDFILE_API)
225 #include <sys/types.h>
226 #include <sys/socket.h>
227 #include <sys/uio.h>
229 ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
231 size_t total=0;
232 struct sf_hdtr hdr;
233 struct iovec hdtrl;
234 size_t hdr_len = 0;
236 hdr->headers = &hdtrl;
237 hdr->hdr_cnt = 1;
238 hdr->trailers = NULL;
239 hdr->trl_cnt = 0;
241 /* Set up the header iovec. */
242 if (header) {
243 hdtrl.iov_base = header->data;
244 hdtrl.iov_len = hdr_len = header->length;
245 } else {
246 hdtrl.iov_base = NULL;
247 hdtrl.iov_len = 0;
250 total = count;
251 while (total + hdtrl.iov_len) {
252 SMB_OFF_T nwritten;
253 int ret;
256 * FreeBSD sendfile returns 0 on success, -1 on error.
257 * Remember, the tofd and fromfd are reversed..... :-).
258 * nwritten includes the header data sent.
261 do {
262 ret = sendfile(fromfd, tofd, offset, total, &hdr, &nwritten, 0);
263 } while (ret == -1 && errno == EINTR);
264 if (ret == -1)
265 return -1;
267 if (nwritten == 0)
268 return -1; /* I think we're at EOF here... */
271 * If this was a short (signal interrupted) write we may need
272 * to subtract it from the header data, or null out the header
273 * data altogether if we wrote more than hdtrl.iov_len bytes.
274 * We change nwritten to be the number of file bytes written.
277 if (hdtrl[0].iov_base && hdtrl.iov_len) {
278 if (nwritten >= hdtrl.iov_len) {
279 nwritten -= hdtrl.iov_len;
280 hdtrl.iov_base = NULL;
281 hdtrl.iov_len = 0;
282 } else {
283 nwritten = 0;
284 hdtrl.iov_base += nwritten;
285 hdtrl.iov_len -= nwritten;
288 total -= nwritten;
289 offset += nwritten;
291 return count + hdr_len;
294 #else /* No sendfile implementation. Return error. */
296 ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
298 /* No sendfile syscall. */
299 errno = ENOSYS;
300 return -1;
302 #endif