Annotation of embedaddon/lighttpd/src/network_linux_sendfile.c, revision 1.1.1.3
1.1.1.3 ! misho 1: #include "first.h"
! 2:
1.1 misho 3: #include "network_backends.h"
4:
1.1.1.3 ! misho 5: #if defined(USE_LINUX_SENDFILE)
1.1 misho 6:
7: #include "network.h"
8: #include "log.h"
9:
1.1.1.3 ! misho 10: #include <sys/sendfile.h>
1.1 misho 11:
12: #include <errno.h>
13: #include <string.h>
14:
1.1.1.3 ! misho 15: int network_write_file_chunk_sendfile(server *srv, connection *con, int fd, chunkqueue *cq, off_t *p_max_bytes) {
! 16: chunk* const c = cq->first;
! 17: ssize_t r;
! 18: off_t offset;
! 19: off_t toSend;
! 20:
! 21: force_assert(NULL != c);
! 22: force_assert(FILE_CHUNK == c->type);
! 23: force_assert(c->offset >= 0 && c->offset <= c->file.length);
! 24:
! 25: offset = c->file.start + c->offset;
! 26: toSend = c->file.length - c->offset;
! 27: if (toSend > *p_max_bytes) toSend = *p_max_bytes;
! 28:
! 29: if (0 == toSend) {
! 30: chunkqueue_remove_finished_chunks(cq);
! 31: return 0;
! 32: }
1.1 misho 33:
1.1.1.3 ! misho 34: if (0 != network_open_file_chunk(srv, con, cq)) return -1;
1.1 misho 35:
1.1.1.3 ! misho 36: if (-1 == (r = sendfile(fd, c->file.fd, &offset, toSend))) {
! 37: switch (errno) {
! 38: case EAGAIN:
! 39: case EINTR:
1.1 misho 40: break;
1.1.1.3 ! misho 41: case EPIPE:
! 42: case ECONNRESET:
! 43: return -2;
! 44: case EINVAL:
! 45: case ENOSYS:
! 46: #if defined(ENOTSUP) \
! 47: && (!defined(EOPNOTSUPP) || EOPNOTSUPP != ENOTSUP)
! 48: case ENOTSUP:
! 49: #endif
! 50: #ifdef EOPNOTSUPP
! 51: case EOPNOTSUPP:
! 52: #endif
! 53: #ifdef ESOCKTNOSUPPORT
! 54: case ESOCKTNOSUPPORT:
! 55: #endif
! 56: #ifdef EAFNOSUPPORT
! 57: case EAFNOSUPPORT:
! 58: #endif
! 59: #ifdef USE_MMAP
! 60: return network_write_file_chunk_mmap(srv, con, fd, cq, p_max_bytes);
! 61: #else
! 62: return network_write_file_chunk_no_mmap(srv, con, fd, cq, p_max_bytes);
! 63: #endif
1.1 misho 64: default:
1.1.1.3 ! misho 65: log_error_write(srv, __FILE__, __LINE__, "ssd",
! 66: "sendfile failed:", strerror(errno), fd);
1.1 misho 67: return -1;
68: }
1.1.1.3 ! misho 69: }
1.1 misho 70:
1.1.1.3 ! misho 71: if (r >= 0) {
! 72: chunkqueue_mark_written(cq, r);
! 73: *p_max_bytes -= r;
1.1 misho 74: }
75:
1.1.1.3 ! misho 76: return (r > 0 && r == toSend) ? 0 : -3;
1.1 misho 77: }
78:
1.1.1.3 ! misho 79: #endif /* USE_LINUX_SENDFILE */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>