diff options
author | Jeremy Allison <jra@samba.org> | 2018-07-18 15:36:47 -0700 |
---|---|---|
committer | Karolin Seeger <kseeger@samba.org> | 2018-08-13 12:56:36 +0200 |
commit | b21e833e83281f58b734296e965c38aa676babdb (patch) | |
tree | fbb9af92217826bac2d16b7bfcc0ea2a96c0945a /source3 | |
parent | 4e0c9718b0fd02117e7e22b5d191c9750809c4ce (diff) | |
download | samba-b21e833e83281f58b734296e965c38aa676babdb.tar.gz |
s3: smbd: Fix HPUX 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 d222caa449d9c00bb2dd9da6c79ea509960d47c6)
Diffstat (limited to 'source3')
-rw-r--r-- | source3/lib/sendfile.c | 56 |
1 files changed, 50 insertions, 6 deletions
diff --git a/source3/lib/sendfile.c b/source3/lib/sendfile.c index db66bf72d16..05e9a9b7cbd 100644 --- a/source3/lib/sendfile.c +++ b/source3/lib/sendfile.c @@ -294,6 +294,9 @@ ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, off_t offset 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. */ @@ -319,11 +322,31 @@ ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, off_t offset do { nwritten = sendfile(tofd, fromfd, offset, total, &hdtrl[0], 0); - } while (nwritten == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)); - if (nwritten == -1) - return -1; - if (nwritten == 0) - return -1; /* I think we're at EOF here... */ + } while (nwritten == -1 && errno == EINTR); + 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 @@ -347,7 +370,28 @@ ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, off_t offset 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) || defined(DARWIN_SENDFILE_API) |