[core] setrlimit max-fds <= rlim_max for non-root (fixes #2723)
[lighttpd.git] / src / network_linux_sendfile.c
blob8d5b91039d97b54e260bd20da9c5a374f77897a3
1 #include "first.h"
3 #include "network_backends.h"
5 #if defined(USE_LINUX_SENDFILE)
7 #include "network.h"
8 #include "log.h"
10 #include <sys/sendfile.h>
12 #include <errno.h>
13 #include <string.h>
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;
21 force_assert(NULL != c);
22 force_assert(FILE_CHUNK == c->type);
23 force_assert(c->offset >= 0 && c->offset <= c->file.length);
25 offset = c->file.start + c->offset;
26 toSend = c->file.length - c->offset;
27 if (toSend > *p_max_bytes) toSend = *p_max_bytes;
29 if (0 == toSend) {
30 chunkqueue_remove_finished_chunks(cq);
31 return 0;
34 if (0 != network_open_file_chunk(srv, con, cq)) return -1;
36 if (-1 == (r = sendfile(fd, c->file.fd, &offset, toSend))) {
37 switch (errno) {
38 case EAGAIN:
39 case EINTR:
40 break;
41 case EPIPE:
42 case ECONNRESET:
43 return -2;
44 default:
45 log_error_write(srv, __FILE__, __LINE__, "ssd",
46 "sendfile failed:", strerror(errno), fd);
47 return -1;
51 if (r >= 0) {
52 chunkqueue_mark_written(cq, r);
53 *p_max_bytes -= r;
56 return (r > 0 && r == toSend) ? 0 : -3;
59 #endif /* USE_LINUX_SENDFILE */