[core] remove some unused header includes
[lighttpd.git] / src / network_linux_sendfile.c
bloba15ef2526d1b39cfe0e7ba9ff70f7efeb2daf7bb
1 #include "first.h"
3 #include "network_backends.h"
5 #if defined(USE_LINUX_SENDFILE)
7 #include "log.h"
9 #include <sys/sendfile.h>
11 #include <errno.h>
12 #include <string.h>
14 int network_write_file_chunk_sendfile(server *srv, connection *con, int fd, chunkqueue *cq, off_t *p_max_bytes) {
15 chunk* const c = cq->first;
16 ssize_t r;
17 off_t offset;
18 off_t toSend;
20 force_assert(NULL != c);
21 force_assert(FILE_CHUNK == c->type);
22 force_assert(c->offset >= 0 && c->offset <= c->file.length);
24 offset = c->file.start + c->offset;
25 toSend = c->file.length - c->offset;
26 if (toSend > *p_max_bytes) toSend = *p_max_bytes;
28 if (0 == toSend) {
29 chunkqueue_remove_finished_chunks(cq);
30 return 0;
33 if (0 != chunkqueue_open_file_chunk(srv, cq)) return -1;
35 if (-1 == (r = sendfile(fd, c->file.fd, &offset, toSend))) {
36 switch (errno) {
37 case EAGAIN:
38 case EINTR:
39 break;
40 case EPIPE:
41 case ECONNRESET:
42 return -2;
43 case EINVAL:
44 case ENOSYS:
45 #if defined(ENOTSUP) \
46 && (!defined(EOPNOTSUPP) || EOPNOTSUPP != ENOTSUP)
47 case ENOTSUP:
48 #endif
49 #ifdef EOPNOTSUPP
50 case EOPNOTSUPP:
51 #endif
52 #ifdef ESOCKTNOSUPPORT
53 case ESOCKTNOSUPPORT:
54 #endif
55 #ifdef EAFNOSUPPORT
56 case EAFNOSUPPORT:
57 #endif
58 #ifdef USE_MMAP
59 return network_write_file_chunk_mmap(srv, con, fd, cq, p_max_bytes);
60 #else
61 return network_write_file_chunk_no_mmap(srv, con, fd, cq, p_max_bytes);
62 #endif
63 default:
64 log_error_write(srv, __FILE__, __LINE__, "ssd",
65 "sendfile failed:", strerror(errno), fd);
66 return -1;
70 if (r >= 0) {
71 chunkqueue_mark_written(cq, r);
72 *p_max_bytes -= r;
75 return (r > 0 && r == toSend) ? 0 : -3;
78 #endif /* USE_LINUX_SENDFILE */