diff options
author | Jeremy Allison <jra@samba.org> | 2018-07-18 15:44:34 -0700 |
---|---|---|
committer | Karolin Seeger <kseeger@samba.org> | 2018-07-28 06:16:16 +0200 |
commit | 7bf15686b60c35c853ae469906baa7d5ed51e4b7 (patch) | |
tree | 512d35b1f620d21938f87e568901c30117863348 /source3/lib | |
parent | adb7d6a1f465dcf39e68cdaa3980a0bf65ca35b4 (diff) | |
download | samba-7bf15686b60c35c853ae469906baa7d5ed51e4b7.tar.gz |
s3: smbd: Fix FreeBSD 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 456e520a3be7e4b54f1f144324c3671b8f6e35ea)
Diffstat (limited to 'source3/lib')
-rw-r--r-- | source3/lib/sendfile.c | 48 |
1 files changed, 44 insertions, 4 deletions
diff --git a/source3/lib/sendfile.c b/source3/lib/sendfile.c index 05e9a9b7cbd..aa115948501 100644 --- a/source3/lib/sendfile.c +++ b/source3/lib/sendfile.c @@ -405,9 +405,11 @@ ssize_t sys_sendfile(int tofd, int fromfd, { struct sf_hdtr sf_header = {0}; struct iovec io_header = {0}; + int old_flags = 0; off_t nwritten; - int ret; + ssize_t ret = -1; + bool socket_flags_changed = false; if (header) { sf_header.headers = &io_header; @@ -428,9 +430,26 @@ ssize_t sys_sendfile(int tofd, int fromfd, #else ret = sendfile(fromfd, tofd, offset, count, &sf_header, &nwritten, 0); #endif - if (ret == -1 && errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK) { + 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. */ - return -1; + ret = -1; + goto out; } if (nwritten == 0) { @@ -457,7 +476,28 @@ ssize_t sys_sendfile(int tofd, int fromfd, count -= nwritten; } - return nwritten; + 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) |