1#include "network_backends.h"
2
3#if defined(USE_SOLARIS_SENDFILEV)
4
5#include "network.h"
6#include "log.h"
7
8#include <sys/sendfile.h>
9
10#include <errno.h>
11#include <string.h>
12
13/**
14 * a very simple sendfilev() interface for solaris which can be optimised a lot more
15 * as solaris sendfilev() supports 'sending everythin in one syscall()'
16 */
17
18int network_write_file_chunk_sendfile(server *srv, connection *con, int fd, chunkqueue *cq, off_t *p_max_bytes) {
19	chunk* const c = cq->first;
20	off_t offset;
21	off_t toSend;
22	size_t written = 0;
23	int r;
24	sendfilevec_t fvec;
25
26	force_assert(NULL != c);
27	force_assert(FILE_CHUNK == c->type);
28	force_assert(c->offset >= 0 && c->offset <= c->file.length);
29
30	offset = c->file.start + c->offset;
31	toSend = c->file.length - c->offset;
32	if (toSend > *p_max_bytes) toSend = *p_max_bytes;
33
34	if (0 == toSend) {
35		chunkqueue_remove_finished_chunks(cq);
36		return 0;
37	}
38
39	if (0 != network_open_file_chunk(srv, con, cq)) return -1;
40
41	fvec.sfv_fd = c->file.fd;
42	fvec.sfv_flag = 0;
43	fvec.sfv_off = offset;
44	fvec.sfv_len = toSend;
45
46	/* Solaris sendfilev() */
47
48	if (-1 == (r = sendfilev(fd, &fvec, 1, &written))) {
49		switch(errno) {
50		case EAGAIN:
51		case EINTR:
52			/* for EAGAIN/EINTR written still contains the sent bytes */
53			break; /* try again later */
54		case EPIPE:
55		case ENOTCONN:
56			return -2;
57		default:
58			log_error_write(srv, __FILE__, __LINE__, "ssd", "sendfile: ", strerror(errno), errno);
59			return -1;
60		}
61	}
62
63	if (written >= 0) {
64		chunkqueue_mark_written(cq, written);
65		*p_max_bytes -= written;
66	}
67
68	return (r >= 0 && (off_t) written == toSend) ? 0 : -3;
69}
70
71#endif /* USE_SOLARIS_SENDFILEV */
72