From 6690b3c7cf6c3463ed12bc0bbc5b7228ed356d50 Mon Sep 17 00:00:00 2001 From: Emil Mikulic Date: Wed, 13 Dec 2006 13:55:48 +0000 Subject: [PATCH] If sendfile blocks and sends zero bytes, treat it as blocking and not end-of-file. This is a really rare corner case. --- trunk/darkhttpd.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/trunk/darkhttpd.c b/trunk/darkhttpd.c index 3f397ba..a14d270 100644 --- a/trunk/darkhttpd.c +++ b/trunk/darkhttpd.c @@ -2126,14 +2126,21 @@ static ssize_t send_from_file(const int s, const int fd, { #ifdef __FreeBSD__ off_t sent; - if (sendfile(fd, s, ofs, size, NULL, &sent, 0) == -1) - { + int ret = sendfile(fd, s, ofs, size, NULL, &sent, 0); + + /* It is possible for sendfile to send zero bytes due to a blocking + * condition. Handle this correctly. + */ + if (ret == -1) if (errno == EAGAIN) - return sent; + if (sent == 0) + return -1; + else + return sent; else return -1; - } - else return size; + else + return size; #else #if defined(__linux) || defined(__sun__) return sendfile(s, fd, &ofs, size);