[core] clean up srv before exiting for lighttpd -[vVh]
[lighttpd/svnmirror.git] / src / network_freebsd_sendfile.c
blob1ddf8706ba5bfada8ba9a1fd56a06f7fda25140c
1 #include "first.h"
3 #include "network_backends.h"
5 #if defined(USE_FREEBSD_SENDFILE)
7 #include "network.h"
8 #include "log.h"
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <sys/uio.h>
14 #include <errno.h>
15 #include <string.h>
17 int network_write_file_chunk_sendfile(server *srv, connection *con, int fd, chunkqueue *cq, off_t *p_max_bytes) {
18 chunk* const c = cq->first;
19 off_t offset, written = 0;
20 off_t toSend;
21 int r;
23 force_assert(NULL != c);
24 force_assert(FILE_CHUNK == c->type);
25 force_assert(c->offset >= 0 && c->offset <= c->file.length);
27 offset = c->file.start + c->offset;
28 toSend = c->file.length - c->offset;
29 if (toSend > *p_max_bytes) toSend = *p_max_bytes;
31 if (0 == toSend) {
32 chunkqueue_remove_finished_chunks(cq);
33 return 0;
36 if (0 != network_open_file_chunk(srv, con, cq)) return -1;
38 /* FreeBSD sendfile() */
39 if (-1 == (r = sendfile(c->file.fd, fd, offset, toSend, NULL, &written, 0))) {
40 switch(errno) {
41 case EAGAIN:
42 case EINTR:
43 /* for EAGAIN/EINTR written still contains the sent bytes */
44 break; /* try again later */
45 case EPIPE:
46 case ENOTCONN:
47 return -2;
48 default:
49 log_error_write(srv, __FILE__, __LINE__, "ssd", "sendfile: ", strerror(errno), errno);
50 return -1;
54 if (written >= 0) {
55 chunkqueue_mark_written(cq, written);
56 *p_max_bytes -= written;
59 return (r >= 0 && written == toSend) ? 0 : -3;
62 #endif /* USE_FREEBSD_SENDFILE */