summaryrefslogtreecommitdiff
path: root/src/network_linux_sendfile.c
diff options
context:
space:
mode:
authorArno Töll <arno@debian.org>2012-11-21 23:03:42 +0100
committerArno Töll <arno@debian.org>2012-11-21 23:03:42 +0100
commit1b24d86e6b2179692e60091dec59f797cd308b9e (patch)
treec5502d40d82fdef69ab9c1e4ed69904fefea9ac1 /src/network_linux_sendfile.c
parent6a6df8dc258631c6eaccb03bf08845241f6dfa4a (diff)
downloadlighttpd-1b24d86e6b2179692e60091dec59f797cd308b9e.tar.gz
Imported Upstream version 1.4.13upstream/1.4.13
Diffstat (limited to 'src/network_linux_sendfile.c')
-rw-r--r--src/network_linux_sendfile.c82
1 files changed, 43 insertions, 39 deletions
diff --git a/src/network_linux_sendfile.c b/src/network_linux_sendfile.c
index 580ba87..6586efb 100644
--- a/src/network_linux_sendfile.c
+++ b/src/network_linux_sendfile.c
@@ -29,32 +29,32 @@
int network_write_chunkqueue_linuxsendfile(server *srv, connection *con, int fd, chunkqueue *cq) {
chunk *c;
size_t chunks_written = 0;
-
+
for(c = cq->first; c; c = c->next, chunks_written++) {
int chunk_finished = 0;
-
+
switch(c->type) {
case MEM_CHUNK: {
char * offset;
size_t toSend;
ssize_t r;
-
+
size_t num_chunks, i;
struct iovec chunks[UIO_MAXIOV];
chunk *tc;
size_t num_bytes = 0;
-
+
/* we can't send more then SSIZE_MAX bytes in one chunk */
-
- /* build writev list
- *
+
+ /* build writev list
+ *
* 1. limit: num_chunks < UIO_MAXIOV
* 2. limit: num_bytes < SSIZE_MAX
*/
- for (num_chunks = 0, tc = c;
- tc && tc->type == MEM_CHUNK && num_chunks < UIO_MAXIOV;
+ for (num_chunks = 0, tc = c;
+ tc && tc->type == MEM_CHUNK && num_chunks < UIO_MAXIOV;
tc = tc->next, num_chunks++);
-
+
for (tc = c, i = 0; i < num_chunks; tc = tc->next, i++) {
if (tc->mem->used == 0) {
chunks[i].iov_base = tc->mem->ptr;
@@ -62,24 +62,24 @@ int network_write_chunkqueue_linuxsendfile(server *srv, connection *con, int fd,
} else {
offset = tc->mem->ptr + tc->offset;
toSend = tc->mem->used - 1 - tc->offset;
-
+
chunks[i].iov_base = offset;
-
+
/* protect the return value of writev() */
if (toSend > SSIZE_MAX ||
num_bytes + toSend > SSIZE_MAX) {
chunks[i].iov_len = SSIZE_MAX - num_bytes;
-
+
num_chunks = i + 1;
break;
} else {
chunks[i].iov_len = toSend;
}
-
+
num_bytes += toSend;
}
}
-
+
if ((r = writev(fd, chunks, num_chunks)) < 0) {
switch (errno) {
case EAGAIN:
@@ -90,13 +90,13 @@ int network_write_chunkqueue_linuxsendfile(server *srv, connection *con, int fd,
case ECONNRESET:
return -2;
default:
- log_error_write(srv, __FILE__, __LINE__, "ssd",
+ log_error_write(srv, __FILE__, __LINE__, "ssd",
"writev failed:", strerror(errno), fd);
-
+
return -1;
}
}
-
+
/* check which chunks have been written */
cq->bytes_out += r;
@@ -105,7 +105,7 @@ int network_write_chunkqueue_linuxsendfile(server *srv, connection *con, int fd,
/* written */
r -= chunks[i].iov_len;
tc->offset += chunks[i].iov_len;
-
+
if (chunk_finished) {
/* skip the chunks from further touches */
chunks_written++;
@@ -116,14 +116,14 @@ int network_write_chunkqueue_linuxsendfile(server *srv, connection *con, int fd,
}
} else {
/* partially written */
-
+
tc->offset += r;
chunk_finished = 0;
-
+
break;
}
}
-
+
break;
}
case FILE_CHUNK: {
@@ -131,17 +131,17 @@ int network_write_chunkqueue_linuxsendfile(server *srv, connection *con, int fd,
off_t offset;
size_t toSend;
stat_cache_entry *sce = NULL;
-
+
offset = c->file.start + c->offset;
/* limit the toSend to 2^31-1 bytes in a chunk */
- toSend = c->file.length - c->offset > ((1 << 30) - 1) ?
+ toSend = c->file.length - c->offset > ((1 << 30) - 1) ?
((1 << 30) - 1) : c->file.length - c->offset;
-
- /* open file if not already opened */
+
+ /* open file if not already opened */
if (-1 == c->file.fd) {
if (-1 == (c->file.fd = open(c->file.name->ptr, O_RDONLY))) {
log_error_write(srv, __FILE__, __LINE__, "ss", "open failed: ", strerror(errno));
-
+
return -1;
}
#ifdef FD_CLOEXEC
@@ -151,7 +151,7 @@ int network_write_chunkqueue_linuxsendfile(server *srv, connection *con, int fd,
/* tell the kernel that we want to stream the file */
if (-1 == posix_fadvise(c->file.fd, 0, 0, POSIX_FADV_SEQUENTIAL)) {
if (ENOSYS != errno) {
- log_error_write(srv, __FILE__, __LINE__, "ssd",
+ log_error_write(srv, __FILE__, __LINE__, "ssd",
"posix_fadvise failed:", strerror(errno), c->file.fd);
}
}
@@ -168,18 +168,19 @@ int network_write_chunkqueue_linuxsendfile(server *srv, connection *con, int fd,
case ECONNRESET:
return -2;
default:
- log_error_write(srv, __FILE__, __LINE__, "ssd",
+ log_error_write(srv, __FILE__, __LINE__, "ssd",
"sendfile failed:", strerror(errno), fd);
return -1;
}
}
if (r == 0) {
+ int oerrno = errno;
/* We got an event to write but we wrote nothing
*
* - the file shrinked -> error
* - the remote side closed inbetween -> remote-close */
-
+
if (HANDLER_ERROR == stat_cache_get_entry(srv, con, c->file.name, &sce)) {
/* file is gone ? */
return -1;
@@ -187,31 +188,34 @@ int network_write_chunkqueue_linuxsendfile(server *srv, connection *con, int fd,
if (offset > sce->st.st_size) {
/* file shrinked, close the connection */
+ errno = oerrno;
+
return -1;
}
+ errno = oerrno;
return -2;
}
#ifdef HAVE_POSIX_FADVISE
#if 0
#define K * 1024
-#define M * 1024 K
+#define M * 1024 K
#define READ_AHEAD 4 M
/* check if we need a new chunk */
if ((c->offset & ~(READ_AHEAD - 1)) != ((c->offset + r) & ~(READ_AHEAD - 1))) {
/* tell the kernel that we want to stream the file */
if (-1 == posix_fadvise(c->file.fd, (c->offset + r) & ~(READ_AHEAD - 1), READ_AHEAD, POSIX_FADV_NOREUSE)) {
- log_error_write(srv, __FILE__, __LINE__, "ssd",
+ log_error_write(srv, __FILE__, __LINE__, "ssd",
"posix_fadvise failed:", strerror(errno), c->file.fd);
}
}
#endif
#endif
-
+
c->offset += r;
cq->bytes_out += r;
-
+
if (c->offset == c->file.length) {
chunk_finished = 1;
@@ -222,19 +226,19 @@ int network_write_chunkqueue_linuxsendfile(server *srv, connection *con, int fd,
c->file.fd = -1;
}
}
-
+
break;
}
default:
-
+
log_error_write(srv, __FILE__, __LINE__, "ds", c, "type not known");
-
+
return -1;
}
-
+
if (!chunk_finished) {
/* not finished yet */
-
+
break;
}
}