summaryrefslogtreecommitdiff
path: root/source3/lib
diff options
context:
space:
mode:
authorJeremy Allison <jra@samba.org>2018-07-18 15:29:37 -0700
committerKarolin Seeger <kseeger@samba.org>2018-07-28 06:16:16 +0200
commit2c58fbdec214d33561e1d1ddad5724d36cfc07f2 (patch)
treeb91ab341690e2c7f6cb2928d8028a14504d5b4e8 /source3/lib
parentc166fa003ae165fc5445dc8b1292719756fa0489 (diff)
downloadsamba-2c58fbdec214d33561e1d1ddad5724d36cfc07f2.tar.gz
s3: smbd: Fix Solaris sendfile() for SMB2. Ensure we don't spin on EAGAIN.
For SMB2 the socket is set non-blocking. Ensure sendfile() calls complete if they return EAGAIN by saving the socket state, setting it blocking, doing the sendfile until completion and then restoring the socket state. BUG: https://bugzilla.samba.org/show_bug.cgi?id=13537 Signed-off-by: Jeremy Allison <jra@samba.org> Reviewed-by: Volker Lendecke <vl@samba.org> (cherry picked from commit 16a587075c8c62c1160869358ca56a133e90247a)
Diffstat (limited to 'source3/lib')
-rw-r--r--source3/lib/sendfile.c56
1 files changed, 50 insertions, 6 deletions
diff --git a/source3/lib/sendfile.c b/source3/lib/sendfile.c
index f7db57acee1..db66bf72d16 100644
--- a/source3/lib/sendfile.c
+++ b/source3/lib/sendfile.c
@@ -169,6 +169,9 @@ ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, off_t offset
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;
@@ -205,17 +208,37 @@ ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, off_t offset
xferred = 0;
nwritten = sendfilev(tofd, vec, sfvcnt, &xferred);
- if (nwritten == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)) {
+ 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
@@ -237,7 +260,28 @@ ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, off_t offset
}
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)