[autobuild] simple check for fork
[lighttpd.git] / src / network_freebsd_sendfile.c
blob11da4e8590f238c0df5c2a11be78e1063e460e25
1 #include "first.h"
3 #include "network_backends.h"
5 #if defined(USE_FREEBSD_SENDFILE)
7 #include "log.h"
9 #include <sys/types.h>
10 #include <sys/socket.h>
11 #include <sys/uio.h>
13 #include <errno.h>
14 #include <string.h>
16 int network_write_file_chunk_sendfile(server *srv, connection *con, int fd, chunkqueue *cq, off_t *p_max_bytes) {
17 chunk* const c = cq->first;
18 off_t offset, written = 0;
19 off_t toSend;
20 int r;
22 force_assert(NULL != c);
23 force_assert(FILE_CHUNK == c->type);
24 force_assert(c->offset >= 0 && c->offset <= c->file.length);
26 offset = c->file.start + c->offset;
27 toSend = c->file.length - c->offset;
28 if (toSend > *p_max_bytes) toSend = *p_max_bytes;
30 if (0 == toSend) {
31 chunkqueue_remove_finished_chunks(cq);
32 return 0;
35 if (0 != chunkqueue_open_file_chunk(srv, cq)) return -1;
37 /* FreeBSD sendfile() */
38 if (-1 == (r = sendfile(c->file.fd, fd, offset, toSend, NULL, &written, 0))) {
39 switch(errno) {
40 case EAGAIN:
41 case EINTR:
42 /* for EAGAIN/EINTR written still contains the sent bytes */
43 break; /* try again later */
44 case EPIPE:
45 case ENOTCONN:
46 return -2;
47 case EINVAL:
48 case ENOSYS:
49 #if defined(ENOTSUP) \
50 && (!defined(EOPNOTSUPP) || EOPNOTSUPP != ENOTSUP)
51 case ENOTSUP:
52 #endif
53 #ifdef EOPNOTSUPP
54 case EOPNOTSUPP:
55 #endif
56 #ifdef ESOCKTNOSUPPORT
57 case ESOCKTNOSUPPORT:
58 #endif
59 #ifdef EAFNOSUPPORT
60 case EAFNOSUPPORT:
61 #endif
62 #ifdef USE_MMAP
63 return network_write_file_chunk_mmap(srv, con, fd, cq, p_max_bytes);
64 #else
65 return network_write_file_chunk_no_mmap(srv, con, fd, cq, p_max_bytes);
66 #endif
67 default:
68 log_error_write(srv, __FILE__, __LINE__, "ssd", "sendfile: ", strerror(errno), errno);
69 return -1;
73 if (written >= 0) {
74 chunkqueue_mark_written(cq, written);
75 *p_max_bytes -= written;
78 return (r >= 0 && written == toSend) ? 0 : -3;
81 #endif /* USE_FREEBSD_SENDFILE */