2 Unix SMB/Netbios implementation.
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.
29 #if defined(LINUX_SENDFILE_API)
31 #include <sys/sendfile.h>
34 #define MSG_MORE 0x8000
37 ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
44 * Send the header first.
45 * Use MSG_MORE to cork the TCP output until sendfile is called.
49 hdr_len = header->length;
50 while (total < hd_len) {
51 ret = sys_send(tofd, header->data + total,hdr_len - total, MSG_MORE);
62 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_SENDFILE64)
63 nwritten = sendfile64(tofd, fromfd, &offset, total);
65 nwritten = sendfile(tofd, fromfd, &offset, total);
67 } while (nwritten == -1 && errno == EINTR);
71 return -1; /* I think we're at EOF here... */
74 return count + hdr_len;
77 #elif defined(LINUX_BROKEN_SENDFILE_API)
79 #include <sys/sendfile.h>
82 #define MSG_MORE 0x8000
85 ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
92 * Fix for broken Linux 2.4 systems with no working sendfile64().
93 * If the offset+count > 2 GB then pretend we don't have the
94 * system call sendfile at all. The upper later catches this
95 * and uses a normal read. JRA.
98 if ((sizeof(SMB_OFF_T) >= 8) && (offset + count > (SMB_OFF_T)0x7FFFFFFF)) {
104 * Send the header first.
105 * Use MSG_MORE to cork the TCP output until sendfile is called.
109 hdr_len = header->length;
110 while (total < hd_len) {
111 ret = sys_send(tofd, header->data + total,hdr_len - total, MSG_MORE);
122 nwritten = sendfile(tofd, fromfd, &offset, total);
123 } while (nwritten == -1 && errno == EINTR);
127 return -1; /* I think we're at EOF here... */
130 return count + hdr_len;
134 #elif defined(SOLARIS_SENDFILE_API)
136 /* Hmmm. Can't find Solaris sendfile API docs.... Where is it ? */
137 ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
143 #elif defined(HPUX_SENDFILE_API)
145 #include <sys/socket.h>
148 ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
151 struct iovec hdtrl[2];
155 /* Set up the header/trailer iovec. */
156 hdtrl[0].iov_base = header->data;
157 hdtrl[0].iov_len = hdr_len = header->length;
159 hdtrl[0].iov_base = NULL;
160 hdtrl[0].iov_len = hdr_len = 0;
162 hdtrl[1].iov_base = NULL;
163 hdtrl[1].iov_base = 0;
166 while (total + hdtrl[0].iov_len) {
170 * HPUX guarantees that if any data was written before
171 * a signal interrupt then sendfile returns the number of
172 * bytes written (which may be less than requested) not -1.
173 * nwritten includes the header data sent.
177 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_SENDFILE64)
178 nwritten = sendfile64(tofd, fromfd, offset, total, &hdtrl[0], 0);
180 nwritten = sendfile(tofd, fromfd, offset, total, &hdtrl[0], 0);
182 } while (nwritten == -1 && errno == EINTR);
186 return -1; /* I think we're at EOF here... */
189 * If this was a short (signal interrupted) write we may need
190 * to subtract it from the header data, or null out the header
191 * data altogether if we wrote more than hdtrl[0].iov_len bytes.
192 * We change nwritten to be the number of file bytes written.
195 if (hdtrl[0].iov_base && hdtrl[0].iov_len) {
196 if (nwritten >= hdtrl[0].iov_len) {
197 nwritten -= hdtrl[0].iov_len;
198 hdtrl[0].iov_base = NULL;
199 hdtrl[0].iov_len = 0;
202 hdtrl[0].iov_base += nwritten;
203 hdtrl[0].iov_len -= nwritten;
209 return count + hdr_len;
212 #elif defined(FREEBSD_SENDFILE_API)
214 #include <sys/types.h>
215 #include <sys/socket.h>
218 ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
225 hdr->headers = &hdtrl;
227 hdr->trailers = NULL;
230 /* Set up the header iovec. */
232 hdtrl.iov_base = header->data;
233 hdtrl.iov_len = hdr_len = header->length;
235 hdtrl.iov_base = NULL;
240 while (total + hdtrl.iov_len) {
245 * FreeBSD sendfile returns 0 on success, -1 on error.
246 * Remember, the tofd and fromfd are reversed..... :-).
247 * nwritten includes the header data sent.
251 ret = sendfile(fromfd, tofd, offset, total, &hdr, &nwritten, 0);
252 } while (ret == -1 && errno == EINTR);
257 return -1; /* I think we're at EOF here... */
260 * If this was a short (signal interrupted) write we may need
261 * to subtract it from the header data, or null out the header
262 * data altogether if we wrote more than hdtrl.iov_len bytes.
263 * We change nwritten to be the number of file bytes written.
266 if (hdtrl[0].iov_base && hdtrl.iov_len) {
267 if (nwritten >= hdtrl.iov_len) {
268 nwritten -= hdtrl.iov_len;
269 hdtrl.iov_base = NULL;
273 hdtrl.iov_base += nwritten;
274 hdtrl.iov_len -= nwritten;
280 return count + hdr_len;
283 #else /* No sendfile implementation. Return error. */
285 ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
287 /* No sendfile syscall. */