2016-03-19 15:14:35 +00:00
|
|
|
#include "first.h"
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
#include "network_backends.h"
|
|
|
|
|
2015-08-22 16:00:59 +00:00
|
|
|
#if defined(USE_WRITEV)
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2009-10-11 14:31:42 +00:00
|
|
|
#include "network.h"
|
|
|
|
#include "log.h"
|
|
|
|
|
2015-08-22 16:00:59 +00:00
|
|
|
#if defined(HAVE_SYS_UIO_H)
|
|
|
|
# include <sys/uio.h>
|
|
|
|
#endif
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2005-10-24 11:37:43 +00:00
|
|
|
|
2013-06-29 12:46:02 +00:00
|
|
|
#if defined(UIO_MAXIOV)
|
2015-08-22 16:00:59 +00:00
|
|
|
# define SYS_MAX_CHUNKS UIO_MAXIOV
|
2013-06-29 12:46:02 +00:00
|
|
|
#elif defined(IOV_MAX)
|
|
|
|
/* new name for UIO_MAXIOV since IEEE Std 1003.1-2001 */
|
2015-08-22 16:00:59 +00:00
|
|
|
# define SYS_MAX_CHUNKS IOV_MAX
|
2013-06-29 12:46:02 +00:00
|
|
|
#elif defined(_XOPEN_IOV_MAX)
|
|
|
|
/* minimum value for sysconf(_SC_IOV_MAX); posix requires this to be at least 16, which is good enough - no need to call sysconf() */
|
2015-08-22 16:00:59 +00:00
|
|
|
# define SYS_MAX_CHUNKS _XOPEN_IOV_MAX
|
2013-06-29 12:46:02 +00:00
|
|
|
#else
|
|
|
|
# error neither UIO_MAXIOV nor IOV_MAX nor _XOPEN_IOV_MAX are defined
|
|
|
|
#endif
|
|
|
|
|
2015-08-22 16:00:59 +00:00
|
|
|
/* allocate iovec[MAX_CHUNKS] on stack, so pick a sane limit:
|
|
|
|
* - each entry will use 1 pointer + 1 size_t
|
|
|
|
* - 32 chunks -> 256 / 512 bytes (32-bit/64-bit pointers)
|
|
|
|
*/
|
|
|
|
#define STACK_MAX_ALLOC_CHUNKS 32
|
|
|
|
#if SYS_MAX_CHUNKS > STACK_MAX_ALLOC_CHUNKS
|
|
|
|
# define MAX_CHUNKS STACK_MAX_ALLOC_CHUNKS
|
2005-10-24 11:37:43 +00:00
|
|
|
#else
|
2015-08-22 16:00:59 +00:00
|
|
|
# define MAX_CHUNKS SYS_MAX_CHUNKS
|
2005-10-18 10:39:38 +00:00
|
|
|
#endif
|
2005-08-08 08:22:06 +00:00
|
|
|
|
2015-08-22 16:00:59 +00:00
|
|
|
int network_writev_mem_chunks(server *srv, connection *con, int fd, chunkqueue *cq, off_t *p_max_bytes) {
|
|
|
|
struct iovec chunks[MAX_CHUNKS];
|
|
|
|
size_t num_chunks;
|
|
|
|
off_t max_bytes = *p_max_bytes;
|
|
|
|
off_t toSend;
|
|
|
|
ssize_t r;
|
|
|
|
UNUSED(con);
|
2005-11-14 10:33:03 +00:00
|
|
|
|
2015-08-22 16:00:59 +00:00
|
|
|
force_assert(NULL != cq->first);
|
|
|
|
force_assert(MEM_CHUNK == cq->first->type);
|
2005-10-24 11:37:43 +00:00
|
|
|
|
2015-08-22 16:00:59 +00:00
|
|
|
{
|
|
|
|
chunk const *c;
|
2005-11-10 12:14:55 +00:00
|
|
|
|
2015-08-22 16:00:59 +00:00
|
|
|
toSend = 0;
|
|
|
|
num_chunks = 0;
|
|
|
|
for (c = cq->first; NULL != c && MEM_CHUNK == c->type && num_chunks < MAX_CHUNKS && toSend < max_bytes; c = c->next) {
|
|
|
|
size_t c_len;
|
2011-08-22 15:12:28 +00:00
|
|
|
|
2015-08-22 16:00:59 +00:00
|
|
|
force_assert(c->offset >= 0 && c->offset <= (off_t)buffer_string_length(c->mem));
|
|
|
|
c_len = buffer_string_length(c->mem) - c->offset;
|
|
|
|
if (c_len > 0) {
|
|
|
|
toSend += c_len;
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2015-08-22 16:00:59 +00:00
|
|
|
chunks[num_chunks].iov_base = c->mem->ptr + c->offset;
|
|
|
|
chunks[num_chunks].iov_len = c_len;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-08-22 16:00:59 +00:00
|
|
|
++num_chunks;
|
2005-09-14 08:00:33 +00:00
|
|
|
}
|
2015-08-22 16:00:59 +00:00
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-08-22 16:00:59 +00:00
|
|
|
if (0 == num_chunks) {
|
|
|
|
chunkqueue_remove_finished_chunks(cq);
|
|
|
|
return 0;
|
|
|
|
}
|
2005-09-26 08:52:37 +00:00
|
|
|
|
2015-08-22 16:00:59 +00:00
|
|
|
r = writev(fd, chunks, num_chunks);
|
|
|
|
|
|
|
|
if (r < 0) switch (errno) {
|
|
|
|
case EAGAIN:
|
|
|
|
case EINTR:
|
|
|
|
break;
|
|
|
|
case EPIPE:
|
|
|
|
case ECONNRESET:
|
|
|
|
return -2;
|
|
|
|
default:
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ssd",
|
|
|
|
"writev failed:", strerror(errno), fd);
|
|
|
|
return -1;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-08-22 16:00:59 +00:00
|
|
|
if (r >= 0) {
|
|
|
|
*p_max_bytes -= r;
|
|
|
|
chunkqueue_mark_written(cq, r);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-08-22 16:00:59 +00:00
|
|
|
return (r > 0 && r == toSend) ? 0 : -3;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-08-22 16:00:59 +00:00
|
|
|
#endif /* USE_WRITEV */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-08-22 16:00:59 +00:00
|
|
|
int network_write_chunkqueue_writev(server *srv, connection *con, int fd, chunkqueue *cq, off_t max_bytes) {
|
|
|
|
while (max_bytes > 0 && NULL != cq->first) {
|
|
|
|
int r = -1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-08-22 16:00:59 +00:00
|
|
|
switch (cq->first->type) {
|
|
|
|
case MEM_CHUNK:
|
|
|
|
r = network_writev_mem_chunks(srv, con, fd, cq, &max_bytes);
|
|
|
|
break;
|
|
|
|
case FILE_CHUNK:
|
|
|
|
r = network_write_file_chunk_mmap(srv, con, fd, cq, &max_bytes);
|
2005-02-20 14:27:00 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-08-22 16:00:59 +00:00
|
|
|
|
|
|
|
if (-3 == r) return 0;
|
|
|
|
if (0 != r) return r;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2011-08-22 15:12:28 +00:00
|
|
|
return 0;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|