From: Jeremy Allison Date: Wed, 18 Jul 2018 22:29:37 +0000 (-0700) Subject: s3: smbd: Fix Solaris sendfile() for SMB2. Ensure we don't spin on EAGAIN. X-Git-Tag: samba-4.7.10~22 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2c58fbdec214d33561e1d1ddad5724d36cfc07f2;p=thirdparty%2Fsamba.git 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 Reviewed-by: Volker Lendecke (cherry picked from commit 16a587075c8c62c1160869358ca56a133e90247a) --- 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)