This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
+ the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
/*
*/
#include "includes.h"
+#include "system/filesys.h"
#if defined(LINUX_SENDFILE_API)
#define MSG_MORE 0x8000
#endif
-ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
+ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, off_t offset, size_t count)
{
size_t total=0;
- ssize_t ret;
+ ssize_t ret = -1;
size_t hdr_len = 0;
+ int old_flags = 0;
+ bool socket_flags_changed = false;
/*
* Send the header first.
hdr_len = header->length;
while (total < hdr_len) {
ret = sys_send(tofd, header->data + total,hdr_len - total, MSG_MORE);
- if (ret == -1)
- return -1;
+ if (ret == -1) {
+ if (errno == EAGAIN || errno == EWOULDBLOCK) {
+ /*
+ * send() must complete before we can
+ * send any other outgoing data on the
+ * socket. Ensure socket is in blocking
+ * mode. For SMB2 by default the socket
+ * is in non-blocking mode.
+ */
+ old_flags = fcntl(tofd, F_GETFL, 0);
+ ret = set_blocking(tofd, true);
+ if (ret == -1) {
+ goto out;
+ }
+ socket_flags_changed = true;
+ continue;
+ }
+ goto out;
+ }
total += ret;
}
}
while (total) {
ssize_t nwritten;
do {
-#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_SENDFILE64)
- nwritten = sendfile64(tofd, fromfd, &offset, total);
-#else
nwritten = sendfile(tofd, fromfd, &offset, total);
-#endif
} while (nwritten == -1 && errno == EINTR);
if (nwritten == -1) {
- if (errno == ENOSYS) {
+ if (errno == EAGAIN || errno == EWOULDBLOCK) {
+ if (socket_flags_changed) {
+ /*
+ * We're already in blocking
+ * mode. This is an error.
+ */
+ ret = -1;
+ goto out;
+ }
+
+ /*
+ * Sendfile must complete before we can
+ * send any other outgoing data on the socket.
+ * Ensure socket is in blocking mode.
+ * For SMB2 by default the socket is in
+ * non-blocking mode.
+ */
+ old_flags = fcntl(tofd, F_GETFL, 0);
+ ret = set_blocking(tofd, true);
+ if (ret == -1) {
+ goto out;
+ }
+ socket_flags_changed = true;
+ continue;
+ }
+
+ if (errno == ENOSYS || errno == EINVAL) {
/* Ok - we're in a world of pain here. We just sent
* the header, but the sendfile failed. We have to
* emulate the sendfile at an upper layer before we
*/
errno = EINTR; /* Normally we can never return this. */
}
- return -1;
+ ret = -1;
+ goto out;
+ }
+ if (nwritten == 0) {
+ /*
+ * EOF, return a short read
+ */
+ ret = hdr_len + (count - total);
+ goto out;
}
- if (nwritten == 0)
- return -1; /* I think we're at EOF here... */
total -= nwritten;
}
- return count + hdr_len;
-}
-
-#elif defined(LINUX_BROKEN_SENDFILE_API)
-
-/*
- * We must use explicit 32 bit types here. This code path means Linux
- * won't do proper 64-bit sendfile. JRA.
- */
-
-extern int32 sendfile (int out_fd, int in_fd, int32 *offset, uint32 count);
-
-
-#ifndef MSG_MORE
-#define MSG_MORE 0x8000
-#endif
-ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
-{
- size_t total=0;
- ssize_t ret;
- ssize_t hdr_len = 0;
- uint32 small_total = 0;
- int32 small_offset;
-
- /*
- * Fix for broken Linux 2.4 systems with no working sendfile64().
- * If the offset+count > 2 GB then pretend we don't have the
- * system call sendfile at all. The upper layer catches this
- * and uses a normal read. JRA.
- */
+ ret = count + hdr_len;
- if ((sizeof(SMB_OFF_T) >= 8) && (offset + count > (SMB_OFF_T)0x7FFFFFFF)) {
- errno = ENOSYS;
- return -1;
- }
+ out:
- /*
- * Send the header first.
- * Use MSG_MORE to cork the TCP output until sendfile is called.
- */
+ if (socket_flags_changed) {
+ int saved_errno;
+ int err;
- if (header) {
- hdr_len = header->length;
- while (total < hdr_len) {
- ret = sys_send(tofd, header->data + total,hdr_len - total, MSG_MORE);
- if (ret == -1)
- return -1;
- total += ret;
+ if (ret == -1) {
+ saved_errno = errno;
}
- }
-
- small_total = (uint32)count;
- small_offset = (int32)offset;
-
- while (small_total) {
- int32 nwritten;
- do {
- nwritten = sendfile(tofd, fromfd, &small_offset, small_total);
- } while (nwritten == -1 && errno == EINTR);
- if (nwritten == -1) {
- if (errno == ENOSYS) {
- /* Ok - we're in a world of pain here. We just sent
- * the header, but the sendfile failed. We have to
- * emulate the sendfile at an upper layer before we
- * disable it's use. So we do something really ugly.
- * We set the errno to a strange value so we can detect
- * this at the upper level and take care of it without
- * layer violation. JRA.
- */
- errno = EINTR; /* Normally we can never return this. */
- }
+ /* Restore the old state of the socket. */
+ err = fcntl(tofd, F_SETFL, old_flags);
+ if (err == -1) {
return -1;
}
- if (nwritten == 0)
- return -1; /* I think we're at EOF here... */
- small_total -= nwritten;
+ if (ret == -1) {
+ errno = saved_errno;
+ }
}
- return count + hdr_len;
-}
+ return ret;
+}
#elif defined(SOLARIS_SENDFILE_API)
#include <sys/sendfile.h>
-ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
+ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, off_t offset, size_t count)
{
int sfvcnt;
size_t total, xferred;
struct sendfilevec vec[2];
ssize_t hdr_len = 0;
+ int old_flags = 0;
+ ssize_t ret = -1;
+ bool socket_flags_changed = false;
if (header) {
sfvcnt = 2;
xferred = 0;
-#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_SENDFILEV64)
- nwritten = sendfilev64(tofd, vec, sfvcnt, &xferred);
-#else
nwritten = sendfilev(tofd, vec, sfvcnt, &xferred);
-#endif
- if (nwritten == -1 && errno == EINTR) {
+ if (nwritten == -1 && errno == EINTR) {
if (xferred == 0)
continue; /* Nothing written yet. */
else
nwritten = xferred;
}
- if (nwritten == -1)
- return -1;
- if (nwritten == 0)
- return -1; /* I think we're at EOF here... */
+ if (nwritten == -1) {
+ if (errno == EAGAIN || errno == EWOULDBLOCK) {
+ /*
+ * Sendfile must complete before we can
+ * send any other outgoing data on the socket.
+ * Ensure socket is in blocking mode.
+ * For SMB2 by default the socket is in
+ * non-blocking mode.
+ */
+ old_flags = fcntl(tofd, F_GETFL, 0);
+ ret = set_blocking(tofd, true);
+ if (ret == -1) {
+ goto out;
+ }
+ socket_flags_changed = true;
+ continue;
+ }
+ ret = -1;
+ goto out;
+ }
+ if (nwritten == 0) {
+ ret = -1;
+ goto out; /* I think we're at EOF here... */
+ }
/*
* If this was a short (signal interrupted) write we may need
}
total -= nwritten;
}
- return count + hdr_len;
+ ret = count + hdr_len;
+
+ out:
+
+ if (socket_flags_changed) {
+ int saved_errno;
+ int err;
+
+ if (ret == -1) {
+ saved_errno = errno;
+ }
+ /* Restore the old state of the socket. */
+ err = fcntl(tofd, F_SETFL, old_flags);
+ if (err == -1) {
+ return -1;
+ }
+ if (ret == -1) {
+ errno = saved_errno;
+ }
+ }
+
+ return ret;
}
#elif defined(HPUX_SENDFILE_API)
#include <sys/socket.h>
#include <sys/uio.h>
-ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
+ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, off_t offset, size_t count)
{
size_t total=0;
struct iovec hdtrl[2];
size_t hdr_len = 0;
+ int old_flags = 0;
+ ssize_t ret = -1;
+ bool socket_flags_changed = false;
if (header) {
/* Set up the header/trailer iovec. */
- hdtrl[0].iov_base = header->data;
+ hdtrl[0].iov_base = (void *)header->data;
hdtrl[0].iov_len = hdr_len = header->length;
} else {
hdtrl[0].iov_base = NULL;
*/
do {
-#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_SENDFILE64)
- nwritten = sendfile64(tofd, fromfd, offset, total, &hdtrl[0], 0);
-#else
nwritten = sendfile(tofd, fromfd, offset, total, &hdtrl[0], 0);
-#endif
} while (nwritten == -1 && errno == EINTR);
- if (nwritten == -1)
- return -1;
- if (nwritten == 0)
- return -1; /* I think we're at EOF here... */
+ if (nwritten == -1) {
+ if (errno == EAGAIN || errno == EWOULDBLOCK) {
+ /*
+ * Sendfile must complete before we can
+ * send any other outgoing data on the socket.
+ * Ensure socket is in blocking mode.
+ * For SMB2 by default the socket is in
+ * non-blocking mode.
+ */
+ old_flags = fcntl(tofd, F_GETFL, 0);
+ ret = set_blocking(tofd, true);
+ if (ret == -1) {
+ goto out;
+ }
+ socket_flags_changed = true;
+ continue;
+ }
+ ret = -1;
+ goto out;
+ }
+ if (nwritten == 0) {
+ ret = -1; /* I think we're at EOF here... */
+ goto out;
+ }
/*
* If this was a short (signal interrupted) write we may need
hdtrl[0].iov_len = 0;
} else {
/* iov_base is defined as a void *... */
- hdtrl[0].iov_base = ((char *)hdtrl[0].iov_base) + nwritten;
+ hdtrl[0].iov_base = (void *)(((char *)hdtrl[0].iov_base) + nwritten);
hdtrl[0].iov_len -= nwritten;
nwritten = 0;
}
total -= nwritten;
offset += nwritten;
}
- return count + hdr_len;
+ ret = count + hdr_len;
+
+ out:
+
+ if (socket_flags_changed) {
+ int saved_errno;
+ int err;
+
+ if (ret == -1) {
+ saved_errno = errno;
+ }
+ /* Restore the old state of the socket. */
+ err = fcntl(tofd, F_SETFL, old_flags);
+ if (err == -1) {
+ return -1;
+ }
+ if (ret == -1) {
+ errno = saved_errno;
+ }
+ }
+
+ return ret;
}
-#elif defined(FREEBSD_SENDFILE_API)
+#elif defined(FREEBSD_SENDFILE_API) || defined(DARWIN_SENDFILE_API)
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
-ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
+ssize_t sys_sendfile(int tofd, int fromfd,
+ const DATA_BLOB *header, off_t offset, size_t count)
{
- size_t total=0;
- struct sf_hdtr hdr;
- struct iovec hdtrl;
- size_t hdr_len = 0;
+ struct sf_hdtr sf_header = {0};
+ struct iovec io_header = {0};
+ int old_flags = 0;
- hdr.headers = &hdtrl;
- hdr.hdr_cnt = 1;
- hdr.trailers = NULL;
- hdr.trl_cnt = 0;
+ off_t nwritten;
+ ssize_t ret = -1;
+ bool socket_flags_changed = false;
- /* Set up the header iovec. */
if (header) {
- hdtrl.iov_base = header->data;
- hdtrl.iov_len = hdr_len = header->length;
- } else {
- hdtrl.iov_base = NULL;
- hdtrl.iov_len = 0;
+ sf_header.headers = &io_header;
+ sf_header.hdr_cnt = 1;
+ io_header.iov_base = header->data;
+ io_header.iov_len = header->length;
+ sf_header.trailers = NULL;
+ sf_header.trl_cnt = 0;
}
- total = count;
- while (total + hdtrl.iov_len) {
- SMB_OFF_T nwritten;
- int ret;
-
- /*
- * FreeBSD sendfile returns 0 on success, -1 on error.
- * Remember, the tofd and fromfd are reversed..... :-).
- * nwritten includes the header data sent.
- */
-
- do {
- ret = sendfile(fromfd, tofd, offset, total, &hdr, &nwritten, 0);
- } while (ret == -1 && errno == EINTR);
- if (ret == -1)
- return -1;
+ while (count != 0) {
- if (nwritten == 0)
- return -1; /* I think we're at EOF here... */
+ nwritten = count;
+#if defined(DARWIN_SENDFILE_API)
+ /* Darwin recycles nwritten as a value-result parameter, apart from that this
+ sendfile implementation is quite the same as the FreeBSD one */
+ ret = sendfile(fromfd, tofd, offset, &nwritten, &sf_header, 0);
+#else
+ ret = sendfile(fromfd, tofd, offset, count, &sf_header, &nwritten, 0);
+#endif
+ if (ret == -1 && errno != EINTR) {
+ if (errno == EAGAIN || errno == EWOULDBLOCK) {
+ /*
+ * Sendfile must complete before we can
+ * send any other outgoing data on the socket.
+ * Ensure socket is in blocking mode.
+ * For SMB2 by default the socket is in
+ * non-blocking mode.
+ */
+ old_flags = fcntl(tofd, F_GETFL, 0);
+ ret = set_blocking(tofd, true);
+ if (ret == -1) {
+ goto out;
+ }
+ socket_flags_changed = true;
+ continue;
+ }
+ /* Send failed, we are toast. */
+ ret = -1;
+ goto out;
+ }
- /*
- * If this was a short (signal interrupted) write we may need
- * to subtract it from the header data, or null out the header
- * data altogether if we wrote more than hdtrl.iov_len bytes.
- * We change nwritten to be the number of file bytes written.
- */
+ if (nwritten == 0) {
+ /* EOF of offset is after EOF. */
+ break;
+ }
- if (hdtrl.iov_base && hdtrl.iov_len) {
- if (nwritten >= hdtrl.iov_len) {
- nwritten -= hdtrl.iov_len;
- hdtrl.iov_base = NULL;
- hdtrl.iov_len = 0;
+ if (sf_header.hdr_cnt) {
+ if (io_header.iov_len <= nwritten) {
+ /* Entire header was sent. */
+ sf_header.headers = NULL;
+ sf_header.hdr_cnt = 0;
+ nwritten -= io_header.iov_len;
} else {
- hdtrl.iov_base += nwritten;
- hdtrl.iov_len -= nwritten;
+ /* Partial header was sent. */
+ io_header.iov_len -= nwritten;
+ io_header.iov_base =
+ ((uint8_t *)io_header.iov_base) + nwritten;
nwritten = 0;
}
}
- total -= nwritten;
+
offset += nwritten;
+ count -= nwritten;
}
- return count + hdr_len;
+
+ ret = nwritten;
+
+ out:
+
+ if (socket_flags_changed) {
+ int saved_errno;
+ int err;
+
+ if (ret == -1) {
+ saved_errno = errno;
+ }
+ /* Restore the old state of the socket. */
+ err = fcntl(tofd, F_SETFL, old_flags);
+ if (err == -1) {
+ return -1;
+ }
+ if (ret == -1) {
+ errno = saved_errno;
+ }
+ }
+
+ return ret;
}
#elif defined(AIX_SENDFILE_API)
/* Contributed by William Jojo <jojowil@hvcc.edu> */
#include <sys/socket.h>
-ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
+ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, off_t offset, size_t count)
{
- size_t total=0;
struct sf_parms hdtrl;
+ int old_flags = 0;
+ ssize_t ret = -1;
+ bool socket_flags_changed = false;
/* Set up the header/trailer struct params. */
if (header) {
hdtrl.file_bytes = count;
while ( hdtrl.file_bytes + hdtrl.header_length ) {
- ssize_t ret;
-
/*
Return Value
*/
do {
ret = send_file(&tofd, &hdtrl, 0);
- } while ( (ret == 1) || (ret == -1 && errno == EINTR) );
- if ( ret == -1 )
+ } while ((ret == 1) || (ret == -1 && errno == EINTR));
+ if ( ret == -1 ) {
+ if (errno == EAGAIN || errno == EWOULDBLOCK) {
+ /*
+ * Sendfile must complete before we can
+ * send any other outgoing data on the socket.
+ * Ensure socket is in blocking mode.
+ * For SMB2 by default the socket is in
+ * non-blocking mode.
+ */
+ old_flags = fcntl(tofd, F_GETFL, 0);
+ ret = set_blocking(tofd, true);
+ if (ret == -1) {
+ goto out;
+ }
+ socket_flags_changed = true;
+ continue;
+ }
+ goto out;
+ }
+ }
+
+ ret = count + header->length;
+
+ out:
+
+ if (socket_flags_changed) {
+ int saved_errno;
+ int err;
+
+ if (ret == -1) {
+ saved_errno = errno;
+ }
+ /* Restore the old state of the socket. */
+ err = fcntl(tofd, F_SETFL, old_flags);
+ if (err == -1) {
return -1;
+ }
+ if (ret == -1) {
+ errno = saved_errno;
+ }
}
- return count + header->length;
+ return ret;
}
/* END AIX SEND_FILE */
#else /* No sendfile implementation. Return error. */
-ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
+ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, off_t offset, size_t count)
{
/* No sendfile syscall. */
errno = ENOSYS;