[network] add darwin-sendfile backend (fixes #2687)
The FreeBSD version of sendfile is already supported. Starting with OS X 10.5, Darwin also supports sendfile, but using a slightly different argument list even though much of the implementation is likely taken from FreeBSD just like the man page is. Add support for darwin's sendfile by introducing a new network_darwin_sendfile.c file that's just a copy of the network_freebsd_sendfile.c file except with the arguments adjusted to compensate for the minor API difference (FreeBSD has separate in and out byte count arguments whereas Darwin has a combined in/out byte count argument). Signed-off-by: Kyle J. McKay <mackyle@gmail.com> git-svn-id: svn://svn.lighttpd.net/lighttpd/branches/lighttpd-1.4.x@3060 152afb58-edef-0310-8abb-c4023f1b3aa9svn/tags/lighttpd-1.4.38
parent
b0ecb4d44b
commit
159ca0c15d
@ -0,0 +1,61 @@
|
||||
#include "network_backends.h"
|
||||
|
||||
#if defined(USE_DARWIN_SENDFILE)
|
||||
|
||||
#include "network.h"
|
||||
#include "log.h"
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/uio.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
int network_write_file_chunk_sendfile(server *srv, connection *con, int fd, chunkqueue *cq, off_t *p_max_bytes) {
|
||||
chunk* const c = cq->first;
|
||||
off_t offset, written = 0;
|
||||
off_t toSend;
|
||||
int r;
|
||||
|
||||
force_assert(NULL != c);
|
||||
force_assert(FILE_CHUNK == c->type);
|
||||
force_assert(c->offset >= 0 && c->offset <= c->file.length);
|
||||
|
||||
offset = c->file.start + c->offset;
|
||||
toSend = c->file.length - c->offset;
|
||||
if (toSend > *p_max_bytes) toSend = *p_max_bytes;
|
||||
|
||||
if (0 == toSend) {
|
||||
chunkqueue_remove_finished_chunks(cq);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (0 != network_open_file_chunk(srv, con, cq)) return -1;
|
||||
|
||||
/* Darwin sendfile() */
|
||||
written = toSend;
|
||||
if (-1 == (r = sendfile(c->file.fd, fd, offset, &written, NULL, 0))) {
|
||||
switch(errno) {
|
||||
case EAGAIN:
|
||||
case EINTR:
|
||||
/* for EAGAIN/EINTR written still contains the sent bytes */
|
||||
break; /* try again later */
|
||||
case EPIPE:
|
||||
case ENOTCONN:
|
||||
return -2;
|
||||
default:
|
||||
log_error_write(srv, __FILE__, __LINE__, "ssd", "sendfile: ", strerror(errno), errno);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (written >= 0) {
|
||||
chunkqueue_mark_written(cq, written);
|
||||
*p_max_bytes -= written;
|
||||
}
|
||||
|
||||
return (r >= 0 && written == toSend) ? 0 : -3;
|
||||
}
|
||||
|
||||
#endif /* USE_DARWIN_SENDFILE */
|
Loading…
Reference in New Issue